How to dynamically embed a winform subformat in a tab control of a main winform?

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

Extra wrinkle: the shape object will be created from a different thread than the main shape.

+4
source share
3 answers

I don’t think it will work when forms are created in different streams, but you usually do this by setting the Form TopLevel property to False, and then adding it to the tab control (or any other parent control).

Edit: You also need to set the Visible property to True, since hidden forms are hidden by default.

Also, I just tried this and it works for something like a button created from another thread. But when I did this using the form, I got an InvalidOperationException.

+4
source

You can simply call Add from the Controls collection on TabPage . You probably want to set the Anchor and Dock properties, respectively, so that the control fills the entire surface (assuming that this is what you want).

I do not think that creating control from another thread is a good route. I am sure that you are quite capable of ridding this hand of thread safety by using appropriate synchronization mechanisms and all that. But all UI elements have a thread-affinity, which means that after creating this UI element, it belongs to the creating thread. You may not have problems with just creating (just calling the constructor), but I take the rule of storing the logic of the all user interface element in the user interface stream literally and strictly.

+2
source

All Articles