Embedding a Form in a Panel

  • Thread starter Thread starter kevin
  • Start date Start date
K

kevin

Hi,

I've done this kind of thing in Win32 using Delphi so I would imagine
there must be an equivalent in WinForms...

How do I embed a form in a panel? This is the most convenient way to
implement an SDI style application (like Outlook) that I know of. Some
hacks that I've seen involve creating an MDI container and creating MDI
child forms that don't have minimize or maximize buttons. If this is
the only solution I'll accept it but I haven't managed to get it to work
properly in .NET either. However, I'd rather not do this - the
embedding would be the best solution.

I would like to be able to replace the embedded form dynamically and I
don't want to have to create user controls to do this. I want to be
able to treat the forms as regular forms in the IDE so that I have the
option of displaying them outside the main window frame as well.

Thanks,
Kevin.
 
How does the article "Not Your Father's Data Binding" answer my
question? I don't see anything in there about embedding a form in a panel.

Thanks,
Kevin.
 
Joe said:
Even in Delphi, you're better off using TFrames for this, rather than
TForms. If you need to display a TFrame free-floating, then just create
a new instance of TForm and parent the frame to it. I do this quite
often at work.

OK, but that's because Delphi has TFrames. .NET doesn't have anything
like that. UserControls are not quite like TFrames unfortunately.
TFrames are much easier to work with!! This is one of the features that
gives Delphi an edge still.
So, I would recommend that you use UserControls instead of Forms.

Are you talking about dynamically creating and destroying UserControls?
So I would create a UserControl for each "form" I wanted to display?
If you're dead-set on using Forms, though, then when using them as
children, you should set their FormBorderStyle to None before setting
their Parent (thus, effectively, turning them into UserControls!).

I'll try to see if this works.

Thanks,
Kevin.
 
kevin said:
I'll try to see if this works.

Thanks for your help! It worked and the full code that was required was:

WinForm1 embeddedForm = new WinForm1();
embeddedForm.TopLevel = false;
embeddedForm.FormBorderStyle = FormBorderStyle.None;
embeddedForm.Parent = this.panel1;
embeddedForm.Show();

Until I added the TopLevel = false it was raising an ArgumentException.

Thanks,
Kevin.
 
AlexS said:
You might need to add also form to Controls collection of the panel.

If the reason for this is that it will be "cleaned up" (disposed?)
automatically then there is no need. I'll be managing that through
other code because I'll be switching between one form and another
dynamically. Is there another reason why I'd need to add it to the
Controls collection? It seems to work fine as it is right now...
 
You might need to add also form to Controls collection of the panel.

Good point on TopLevel!

HTH
Alex
 
OK, but that's because Delphi has TFrames. .NET doesn't have anything
like that. UserControls are not quite like TFrames unfortunately.
TFrames are much easier to work with!! This is one of the features that
gives Delphi an edge still.

Really? As far as I've seen, UserControls are pretty much the
equivalent of TFrames. What are some specific downsides to UserControls
that you're running into?

Are you talking about dynamically creating and destroying UserControls?
So I would create a UserControl for each "form" I wanted to display?

Right. Instead of creating multiple forms (one for each page you want
to be able to show in your master window), you would create the same
number of UserControls. (Just like you would create that number of
TFrames in Delphi. The app I'm doing for work is built this way,
swapping different TFrames in and out of the same master window.)
 
Back
Top