In this article, we will look at MDI in vb.net. Multiple document interface or MDI allows you to view multiple documents at once. MDI for WinForms allows you to display forms in another form. The forms need to be set as either a parent or child. The children are shown inside of the parent. To set the form to a parent, set the IsMdiContainer to true. To set a form as a child you set the MdiParent property to the form that is to be the parent. You can also manually add forms to controls to do this as well.
Basic Example
Create a new VB.NET project for WinForms. Resize the main form to a bigger size and set the IsMdiContainer property to True. Add a menu strip component to the form and add a button with the name btnAddForm and text of Add. Next, add the following code to the form:
Private Sub btnAddForm_Click(sender As Object, e As EventArgs) Handles btnAddForm.Click
Dim frm As New Form() With {.Text = "Form", .MdiParent = Me}
frm.Show
End Sub
Run the program and click the add button and the child form is added to the main form. This form can do everything a normal form can do except display as a dialog.
MDI Without MDI Container Form Example
You can also show a form inside of another form without having to use the IsMdiContainer property. This can be useful if you want to limit the area where the child forms can be displayed. For example, you have some fields and buttons on one side of the parent and forms on the other. Let’s change our example. On the form disable the IsMdiContainer property and remove the menu strip. Add a split container control and to panel 1, add a button with the name of btnAddForm. Add this code:
Private Sub btnAddForm_Click(sender As Object, e As EventArgs) Handles btnAddForm.Click
Dim frm As New Form With {.Text = "Form", .TopLevel = False}
splitContainer1.Panel2.Controls.Add(frm)
frm.Show
End Sub
As you can see the forms can only be in their parent control; they cannot be moved into the other panel where you could put any other control. Please note that you have to set the TopLevel property of the child form to work to false for this to work. This is not limited to just split containers; it can be done with any control groups, panels, etc. Even in another form.
MDI Manager
Now we will make a sample MDI manager. On our main form enable the IsMdiContainer property again and add a menu strip with an add form button and a forms button. It should look something like this:
Next, add the following code:
Dim c As Integer = 1
Private Sub btnAddForm_Click(sender As System.Object, e As System.EventArgs) Handles btnAddForm.Click
Dim frm As New Form With {.Text = "Form" & c, .MdiParent = Me}
Dim btnForm As New ToolStripButton With {.Text = frm.Text}
AddHandler btnForm.Click, Sub(s, ev)
frm.Focus()
End Sub
btnForms.DropDownItems.Add(btnForm)
frm.Show()
c += 1
End Sub
As you can see when you click add it shows a form and adds a button to the form’s drop-down; when clicking the buttons it focuses its form.
Conclusions
As you can see MDI can very useful for apps where you have lots of forms for viewing or editing data. But it’s important to organize your menus and forms.