开发者

Winforms - Embedding form inside a form or container causes form controls to behave differently

开发者 https://www.devze.com 2023-01-11 18:18 出处:网络
(Revised for content quality) We had built forms ba开发者_运维问答sed application for a customer based on their requirements. Toward the end of the project, they decided they wanted the app to functi

(Revised for content quality)

We had built forms ba开发者_运维问答sed application for a customer based on their requirements. Toward the end of the project, they decided they wanted the app to function as a part of another Winforms app in a way that required the screens to be contained within larger forms. They also wanted to dynamically load forms and other UI elements. I decided to try simply loading forms dynamically inside another form, at at the time, wasn't comfortable with changing over all of the forms to controls.

When I load a form dynamically inside another form or container control, the editing behavior of the inner form's components changes. Text editing, in a TextBox or ComboBox, does not allow me to select partial text with the mouse, though I can double click to select all the text in the control.

     FormChild form = new FormChild();
     form.TopLevel = false;
     form.Dock = DockStyle.Fill;
     Controls.Clear();
     Controls.Add(form);
     form.Show();

The controls work as expected when I show the form normally, using Show() or ShowDialog(), but not when nested.

Being a bit new to Winforms from years of MFC, we had experience embedding forms inside MFC controls using FormView without issues, so assumed it would work ok with Winforms. It didn't turn out that way.

SOLUTION: I should have used a User Control in the first place. I refactored every form in the app manually, created blank controls, cut and pasted the InitializeComponent() as well as the logic into a User Control. As for the dynamic components, nowadays I create them at runtime, or do them in IronPython. Four years later I'm looking back at this question, shaking my head at my rookie mistake.

In addition, I've found that controls inside dockable panels work well for the original scenario I was faced with. Specifically, I started using DevExpress' DocumentManager and DockPanel. My inexperience at the time with Winforms got us into trouble; I've left this question in case someone else makes the same mistake.


Is it possible to place all the functionality (including controls) in your form into a User Control? If you want this functionality in a separate form, drop the User Control into an empty form, if you want it as part of an existing form, drop it into that.

This isn't a solution to the problem as you presented it, but maybe this suggestion will help.


Judging from what you have posted, you want to dynamically load some ui elements and only show them when all are done loading? I usually will do it with a `Panel' or user control

[not compile tested code]

Panel p = new Panel();
p.TopLevel = false;
p.Dock = DockStyle.Fill;
p.Controls.Add( button );
// etc
Controls.Clear();
Controls.Add(p);


In short, what you want is possible, just take a look here:

http://social.msdn.microsoft.com/Forums/en/winforms/thread/a5a5ace5-2f88-4ed3-b1e5-ec3500ce1df1

It's all a matter of importing SetParent and using it. There'll be some overhead -- you'll have to deal with size and location of the child location -- but not much of a deal in most scenarios.

0

精彩评论

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