I know it's possible to use a Form
as a child control in another Form.
ChildForm child = new ChildForm();
child.TopLevel = false;
child.FormBorderStyle = FormBorderStyle.None;
child.Show();
containerControl.Controls.Add(child);
I realise this is not what a Form
was designed for. I was wondering what the implications here are, as opposed to using a UserControl
? Are there any real downsides? Any large, unnecessary overheads? Any unexpected behaviours?
On the other hand, are there any advantages of using a form? Is there anything you can't do with a UserControl that you can do with a form, which may be useful if used as a child control?
I understand that both Form
and UserControl
inherit ContainerControl
, but how different do their non-inherited members make them?
My scenario
The reason I'm asking is because I have an existing Form
control, that I'd like to be displayed on its own sometimes (for development and as a little extra feature), but it doesn't need to be. However, I do need the Closing
event of the form (which is not inherited from ContainerControl
) and I need to be able to cancel closing the form if a file has been edited and not saved.
I've also noticed in my findings that a Form
seems to have some transparency options that a UserControl
doesn't seem to have, but I'm not sure if these will work when the form is used as a child control.
if you check the Inheritance Hierarchy in MSDN you find out that both UserControl and Form inherit from ContainerControl so in fact up to their base class nearly everything should be the same.
said so, in general, I think you should avoid using a Form object hosted as a control unless you are dealing with legacy unchangeable code or unless you absolutely want this design so to be able, in certain other cases, to show the form all alone as top level non hosted anywhere.
If you simply need a container to put your other UI controls you can use a UserControl.
in fact everything down to the Win32 level translates to handles and message pumps / WindowProc etc, even a simple TextBox will have an handle and will receive and consume messages taking care of its client and non client area and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.