I am developing an application that has a menubar. On selecting menu items, I got to open differnet windows (Forms).

This all makes the main program have nothing except the menubar. I want that on clicking any menu item, the respective form comes somewhere on the main window only. On close event of the new form, it gers closed - disappears. So, the new form is inside the main form only. New window is not opened. How do I acheive something like this. If I add a Container in the main window, can I add a form in it. Or Do I have to make the new form as a Panel and add it in some how. If it is a Panel,. then how do I add in the main form.

I don't know how do I acheive such a thing in VB.NET. I would appreciate, if anyone can help me out.


Re: Visual Basic Language Form in Form


You should just try the MDI Control, (like you mentioned -the container).

To set the containter, on the main form, go to properties and find "IsMdiContainer", set the value to true.

Now When you click on the menu item in your Designer View, the code will come up, type the following:

Dim Me As New Form 2

Me.MdiParent = Me


Replacing Me with anything you want and Form 2 with the name of your form.

Give this a try.

Re: Visual Basic Language Form in Form


Creating a MDI (multiple document interface) application is one possible route. However, from the way you describe it, it sounds like you actually want those child windows to be UserControls instead of Forms. So you would add new UserControl objects to the project and design them with all the appropriate controls. Then clicking the menu items can load the specified user control and display it on the Form.

Re: Visual Basic Language Form in Form


you dont need to make usercontrols to put forms in other forms.

All you have to do is add a form to another forms controls collection and make sure the form that will be "inside" the other form has its TopLevel property set to false, otherwise you get an exception that you cant add a toplevel window to a container.

Inside Form1, you could put this code:

Code Snippet

Dim F As New Form2

F.TopLevel = False


F.Visible = True

Re: Visual Basic Language Form in Form


Well that's kinda cool - I didn't know you could do that.

Although, I notice that you get some odd behavior using Dock and Anchor properties...

Re: Visual Basic Language Form in Form


Well when using the "form in a form" method, its usually best to use a fixed size child form (or have it maximized inside a panel control) so you can have more control over its sizing/positioning behavior.

The one app I actually use that technique in, I load a form with no border into a panel control. The forms window state is set to maximized, and I control its size by setting properties on the panel control.