• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Windows forms and manually setting the TopLevel property

SunnyD

Belgian Waffler
I'm by no means an expert with .Net, so I'm trying to figure out all the nice tricks and such. One thing that I'm curious about is how to "nest" forms inside of other forms. Basically, imagine a UI where you have several "pages" that are discrete, and you want to use one form as a host container, but not in the MDI manner. In essence, I want discrete pages/UI's for each "panel".

I started playing around and found I couldn't make a form, do the following:

FormX fm = new FormX();
fm.Parent = Form1.panel1;
fm.Show();

That causes an exception saying that FormX is a top-level container. So then I did this:

FormX gm = new FormX();
fm.TopLevel = false;
fm.Parent = Form1.panel1;
fm.Show();

This works fine, but I find nearly no documentation on the TopLevel property, and no examples on how to do this anywhere - which leads me to believe this either isn't good practice or there's a different way it's done, or more likely I just don't understand Windows Forms enough. Is there a different (or better) way this is supposed to be done?
 
Are you using Visual Studio to code in? If so, look at creating a Windows Form class. That will give you access to the Form Designer and will auto generate code to display and use the form you created in the design view.

If I understand your question, you basically want a main window that then has a bunch of smaller windows that you can move around and change properties on but those smaller windows need to stay inside the bounds of the main window and always be on top?

I'm not sure on the best way to do it, but I would listen to the Move/Resize events and do some bounds checking to make sure those windows stay within the main window. Obviously there's a bit more to it, but that's how I would start.
 
Back
Top