How to embed a form in a form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is what I would like to do:
- The splitter window splitting the view to a tree view on the left and
entry dialog on the right. The tree view and splitter are easy and it's
already done.
- It is easy to make a fixed form on the right pane, dock it and enjoy it
working.
However, I would like the form on the right pane to be dynamic, i.e.,
depending on a context of the tree view to display different controls and
being managed by a different form derived class.
In MFC I could accomplish this with child dialogs embedded in a parent
dialog and depending on the tree context flipping the child dialogs
visible/invisible.
I can do the same in .NET by using panels with embedded controls, however,
this way everything must be done in the runtime and I lose advantage of
designing a forms in a design time and taking advantage of .NET studio
wizards.
If I could find a way to embed a child form in a parent form, then it would
solve my problem. Is it possible?
Thanks
 
Yes,

Use reflections to load an assembly of the desired type, and then invoke a
method (common to all child forms) into which you pass the parent panel
control. Within this common method assign the child form to be boarderless,
not toplevel and have a parent set to that of the panel as passed in (and
set the docking to fill), then show the child form.

- Colin
 
Hi Cezar,
Try this following code, I am posting it in VB.net you can convert it to c#

Dim objFrm As New Form1
objFrm.TopLevel = False
pnlForm.Controls.Add(objFrm)
objFrm.Show()

Here Form1 is the form designed at design time. pnlForm is the Panel in the
parent form, in your case the Right side panel.

Regards
Sen
 
Hi Sen,
I was lurking on this thread. Thank you for the code. Works great. My
question is, how to show the form modally? When I try ShowDialog it
says forms that are not top level can not be shown as modal dialog.
But it also does not let me set the TopLevel = true...says parented
controls can not have top level true. Can this be done or is there
another way to prevent users from opening multiple instances of the
form?

TIA,
John
 
Cezar said:
- The splitter window splitting the view to a tree view on the left and
entry dialog on the right. The tree view and splitter are easy and it's
already done.
- It is easy to make a fixed form on the right pane, dock it and enjoy it
working.
However, I would like the form on the right pane to be dynamic, i.e.,
depending on a context of the tree view to display different controls and
being managed by a different form derived class.

Design usercontrols for the different views ("Project" -> "Add user
control...") and place them on the form. No need to worry about embedding
other forms...
 
Your code works wonderful! This is exactly what I needed.
I have to have two such forms, so implementing the following switch
statement does everything:
private void OnChangeForm ( int nForm )
{
switch ( nForm )
{
case 1:
form2.Hide();
form1.Show();
break;
case 2:
form1.Hide();
form2.Show();
break;
}
}

Thanks again
Cezar
 
Back
Top