Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Introduction to Visual Basic .

NET 19

Chapter IV
Creating MDI Applications and Menu Items

Creating an MDI Parent Form

The MDI parent form is at the heart of an MDI application.


It is the container for the multiple documents—the child forms—within an MDI application. It
can be used the IsMDIContainer property to create an MDI parent form. Follow these steps to
create an MDI parent form:

1. Create a new form and open it in the Code window.


2. In the constructor for your form, add the following code:
Me.IsMDIContainer = True
3. Set its WindowState property to Maximized.
www.syngress.com
Creating MDI Child Forms
MDI child forms are forms that operate within an MDI parent form in an MDI
application.
Example:
(i) Creating a MDI Parent Form
1. From the File menu, select New Project.
2. In the Visual Basic Projects list, select the Windows Application template and then
click OK.
3. In the Properties Window, set the IsMDIContainer property to True, and then set
the WindowState property to Maximized.
(ii) Creating a MDI Child Form
1. From the Project menu, select Add Windows Form.
2. In the Local Project Items list, select the Windows Form template and then click
Open.
(iii) Displaying a MDI Child Form
1. Select the MDI parent form.
2. On the Toolbox, select the Win Forms tab and double-click the Button control to put
it on the form.
3. On the MDI parent form, double-click the button. Replace the event handler for the
Click event with the following code to create a new MDI child form when the button
is clicked:
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click
IsMdiContainer = True
Dim frmChild As New Child
frmChild.MdiParent = Me
frmChild.Show()
End Sub
The following code on the parent form closes the active child form:
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnClose.Click
frmMain.ActiveForm.Close()
End Sub
End Class
Introduction to Visual Basic .NET 20

Arranging MDI Child Forms:


MDI parent forms often sport a Window menu with Arrange, Cascade, Tile
Horizontal, and Tile Vertical submenus.

Settings of the MDILayout Enumeration

Setting Description
ArrangeIcons Displays child form icons arranged along the lower portion
of the parent form.
Cascade Displays cascading child forms.
TileHorizontal Displays horizontally tiled child forms.
TileVertical Displays vertically tiled child forms.

To tile child forms horizontally when the user clicks the appropriate menu:
Private Sub btnH_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnH.Click
NewForm.ActiveForm.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Example:1
Create a Main Form and a Child Form
as given blew design.

Main Form

Child Form

Write the following codes in the "frmArrange" Main Form.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnAdd.Click
IsMdiContainer = True
Introduction to Visual Basic .NET 21

Me.WindowState = FormWindowState.Maximized
Dim frmNew As New NewForm
frmNew.MdiParent = Me
frmNew.Show()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnExit.Click
frmArrange.ActiveForm.Close()
End Sub
Write the following codes in the Child Form : "NewForm" .

Private Sub btnH_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnH.Click
NewForm.ActiveForm.LayoutMdi(MdiLayout.TileHorizontal)
End Sub

Private Sub btnV_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnV.Click
NewForm.ActiveForm.LayoutMdi(MdiLayout.TileVertical)
End Sub

Private Sub btnCascade_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnCascade.Click
NewForm.ActiveForm.LayoutMdi(MdiLayout.Cascade)
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Example 2 Creating Main form and Child Form
(i) Creating a Main Form
1. From the File menu, select New Project.
2. In the Visual Basic Projects list, select the Windows Application template
and then click OK.
3. In the Properties Window, set the IsMDIContainer property to False, and
then set the WindowState property to Normal.
(ii) Creating a MDI Child Form
1. From the Project menu, select Add Windows Form.
2. In the Local Project Items list, select the Windows Form template and then
click Open.
Introduction to Visual Basic .NET 22

Creating Menus to a Form

Adding a Menu to a Form at Design Time:

To add a File menu with a File menu item to a form at design time:
1. From the File menu, select New Project.
2. In the Visual Basic Projects list, select the Windows Application template, and then
click OK.
3. In the Toolbox, select the Win Forms tab, and then double-click the MainMenu
control. A menu is added to the form displaying the text Type Here, and the
MainMenu control is added to the component tray.

Type Here

gress.com

4. In the Menu Designer, click the text Type Here to select the menu, and then type
&File

5. Click the area below the File menu item to add another entry to the same menu, and
type E&xit.
The File Menu and the Exit Menu Item
Introduction to Visual Basic .NET 23

Example :
 Add the Menu Items to the Form in the following form design.

Menu Enhancements:
The Windows Forms framework includes four menu enhancements that it can use to
convey information to the user.

Enhancement Description
Check marks Indicate whether a feature is turned on or off (such as whether a ruler is
displayed along the margin of a word-processing application) or to indicate
which of a list of files is being displayed (such as in a Window menu).

Shortcut keys Allow access to menu items using keyboard commands. ( Ctlrl key )
Access keys Allow keyboard navigation of menus (pressing the Alt key, and the
underlined access key chooses the desired menu or menu item).

Separator bars Used to group related commands within a menu and make menus easier to
read.

Adding a check mark to a menu at design time:


Introduction to Visual Basic .NET 24

1. In the Menu Designer, select the menu item.


2. Click the area to the left of the menu item. A check mark appears.
( To remove the check mark, it can remove it by repeating the same steps.)

Adding a shortcut key to a menu item at design time, perform the following steps:
1. Use the View menu to open the Properties window.
2. In the Menu Designer, select the menu item.
3. In the Properties window, set the Shortcut property to one of the values offered in
the drop-down list.

Adding an access key to a menu item at design time:


1. In the Menu Designer, select the menu item.
2. When setting the Text property, enter an ampersand (&) prior to the letter you want to
be underlined as the access key.

For example, typing &File as the Text property of a menu item will result in a menu item that
appears as File.

You might also like