Question

Is there a way that I can hand a Winforms form object to a main form containing a tab control, and have the main form load this form object into one of the tabs?

An additional wrinkle: the form object will be instantiated from a different thread than the main form.

Was it helpful?

Solution

I don't think it will work when the Forms are created on different threads, but you would normally do this by setting the Form's TopLevel property to False and then adding it to the tab page control (or any other parent control).

Edit: You also need to set the Visible property to True because Forms are hidden by default.

Also, I just tried this and it does work for something like a button created from another thread. But when I did it with a Form, I got an InvalidOperationException.

OTHER TIPS

You should be able to just call Add from the Controls collection on the TabPage. You will probably want to set the Anchor and Dock properties accordingly so that the control fills the whole surface (assuming that is what you want).

I do not think creating the control from another thread is a good route though. I am sure you are quite capable of making this hand off thread-safe as far as using the appropriate synchronization mechanisms and whatever. But, all UI elements have thread-affinity which means that once that UI element is created it belongs to the creating thread. You may not have any issues with simply creating (just a constructor call), but I take the rule of keeping all UI element logic on the UI thread quite literally and strictly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top