vbnet
  1. vbnet-mdi-form

Working with Forms and Controls in VB.NET MDI Form

VB.NET MDI (Multiple Document Interface) forms allow you to create applications that can display multiple child forms within a single main form. Child forms can be opened or closed dynamically, and can contain various controls that allow users to interact with the application.

Syntax

To create an MDI form in VB.NET, set the IsMdiContainer property of the main form to True.

Public Class MainForm
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.IsMdiContainer = True
    End Sub
End Class

To open a child form within the MDI form, create an instance of the child form and set its MdiParent property to the main form.

Dim childForm As New ChildForm()
childForm.MdiParent = Me
childForm.Show()

Example

In this example, we create a simple VB.NET MDI form application with two child forms - one for adding numbers and one for subtracting numbers. Each child form contains input fields, buttons, and a result label.

Public Class MainForm
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.IsMdiContainer = True
    End Sub

    Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click
        Dim addForm As New AddForm()
        addForm.MdiParent = Me
        addForm.Show()
    End Sub

    Private Sub SubtractToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubtractToolStripMenuItem.Click
        Dim subForm As New SubForm()
        subForm.MdiParent = Me
        subForm.Show()
    End Sub
End Class

Public Class AddForm
    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        Dim num1 As Integer = Integer.Parse(Num1Field.Text)
        Dim num2 As Integer = Integer.Parse(Num2Field.Text)
        Dim result As Integer = num1 + num2
        ResultLabel.Text = result.ToString()
    End Sub
End Class

Public Class SubForm
    Private Sub SubtractButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubtractButton.Click
        Dim num1 As Integer = Integer.Parse(Num1Field.Text)
        Dim num2 As Integer = Integer.Parse(Num2Field.Text)
        Dim result As Integer = num1 - num2
        ResultLabel.Text = result.ToString()
    End Sub
End Class

Output

The output of this application is a VB.NET MDI form with two child forms. The user can open both child forms and input numbers to either add or subtract.

VB.NET MDI Form Example Output

Explanation

In the above example, we create an MDI form called MainForm with two menu items - "Add" and "Subtract". When the user clicks on either of these menu items, a new child form is created using the AddForm or SubForm classes. Each child form contains input fields for two numbers, buttons to perform the appropriate operation, and a result label to display the result of the operation.

Use

VB.NET MDI forms can be used to create applications that allow the user to perform multiple tasks at the same time within a single window. They are ideal for applications that have multiple processes that need to be managed in a cohesive way.

Important Points

  • VB.NET MDI forms allow you to create applications that can display multiple child forms within a single main form.
  • Child forms are opened and closed dynamically and can contain various controls that allow users to interact with the application.
  • Child forms are created using the Form class and their MdiParent property is set to the main form.
  • The IsMdiContainer property of the main form must be set to True to enable MDI functionality.

Summary

In summary, VB.NET MDI forms allow you to create applications that can display multiple child forms within a single main form. Child forms are created using the Form class and their MdiParent property is set to the main form. The IsMdiContainer property of the main form must be set to True to enable MDI functionality.

Published on: