Creating MDI Applications: Solution Explorer Add - Add Windows Form
Creating MDI Applications: Solution Explorer Add - Add Windows Form
documents at once. Example of an MDI application is Microsoft Excel. Excel allows us to work with several documents at once. In contrast, SDI (Single Document Interface) applications are the applications which allows us to work with a single document at once. Example of a single document application is Microsoft Word in which only one document is visible at a time. Visual Basic .NET provides great support for creating and working with MDI applications. In general, MDI applications are mostly used by financial services organizations where the user needs to work with several documents at once. Creating MDI Applications Let's create an MDI application. Open a new Windows Application in Visual Basic .NET. The application will open with a default form, Form1. Add another form, Form2 to this application by right-clicking on the project name in Solution Explorer window and selecting Add->Add Windows Form. You can add some controls to Form2. For this application we will make From1 as the MDI parent window and Form2 as MDI child window. MDI child forms are important for MDI Applications as users interact mostly through child forms. Select Form1 and in it's Properties Window under the Windows Style section, set the property IsMdiContainer to True. Setting it to true designates this form as an MDI container for the child windows. Once you set that property to true the form changes it's color. Now, from the toolbox drag a MainMenu component onto Form1. We will display child windows when a menu item is clicked. Name the top-level menu item to File with submenu items as New Child Window, Arrange Child Windows and Exit. The whole form should look like the image below.
With this application a new child window is displayed each time the New Child Window menu item is clicked, all child windows will be arranged when you click Arrange Child Windows menu item. To get the desired result, open the code designer window and paste the following code. Public Class Form1 Inherits System.Windows.Forms.Form Dim childForm As Integer = 0 Dim childForms(5) As Form2 'declaring an array to store child windows 'five child windows (Form2) will be displayed #Region " Windows Form Designer generated code " Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load End Sub Private Sub MenuItem2_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MenuItem2.Click childForm += 1 childForms(childForm) = New Form2() childForms(childForm).Text = "ChildForm" & Str(childForm) 'setting title for child windows and incrementing the number with an array childForms(childForm).MdiParent = Me
childForms(childForm).Show() End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MenuItem3.Click Me.LayoutMdi(MdiLayout.Cascade) 'arranging child windows on the parent form with predefined LayoutMdi method 'Different layouts available are, ArrangeIcons, Cascade, TileHorizontal, TileVertical End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal_ e As System.EventArgs) Handles MenuItem4.Click Me.Close() 'closing the application End Sub End Class When you run the application and click "New Child Window" menu item, a new child window is displayed. Five child windows will be displayed as we declared an array of five in code. The image below displays the output.
When you click on "Arrange Child Windows" menu item, all child windows are arranged. It looks like the image below.