开发者

How do I dynamically embed a winform subform into the tab control of a main winform?

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.


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.


Do you want something like Multi Document Interface (MDI) tab page browsing?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜