Write For Us

Menus And MDI Applications

.NET provides support to create menus and MDI applications. You can create menus to enhance a Windows application. You can also display multiple forms at the same time. This functionality is called MDI.


Working with Menus

In the windows environment, you can use menus to enhance the user interface of an application. Menus offer convenient and consistent way to organize related options into a group. In .NET you can create two types of menus:
- Menus that appear on the menu bar
- Context Menus

Menus Appearing on the Menu Bar

The menus that appear on the menu bar contain a list of options that a user can use for various requirements. For example, in word processing application, the File menu has options, such as Open, Save and Close. Using .Net you can create similar menus for your application.

In .NET, the menus that appear on the menu bar are created using the MainMenu object. This object is a collection of MenuItem objects that are used to add individual menu items to the menu bar. These can be added either at design time or at run time. To add at design time, you need to add the MainMenu control from the toolbox and then append individual menu items using the Menu Designer. To add menu items at runtime, you first need to add the MainMenu control from the toolbox at design time and then write code to programmatically add MenuItem objects to the MainMenu collection.

Context Menus

In addition to the menus that appear on the menu bar, .NET supports the creation of context menus to provide users easy access to related options. Context menus are shortcut menus that are displayed when you click the right mouse button on a control, such as a textbox control. Context menus contain the most frequently used menu options. For example you have Cut, Copy, Paste and Delete.

Similar to the menus that appear on the Menu bar, context menu items can also be added to a form either at design time or at run time. To add context menu for a control or a form, you need to add ContextMenu control from the toolbox to the from.

Working with MDI Applications

In addition to menus, Visual Basic .Net supports Multiple Document Interface (MDI) applications. MDI applications allow you to display multiple windows at the same time, with each form sharing a parent-child relationship.

By default, the Windows application that you create using .NET is a Single Document Interface (SDI) application. In SDI application, you can work with only one window at a time. If you need to work with multiple windows, you need to invoke multiple instances of the SDI application, which uses more system resources, such as memory. Microsoft WordPad is an example of an SDI application.

In .NET, you can also create an MDI application. When an application supports multiple windows simultaneously, it is called an MDI application. Microsoft Word is an example of an MDI application. An MDI application consists of two parts, MDI parent form and MDI child form. An MDI application can have one or more MDI parent forms and each MDI parent form can have multiple MDI child forms. An MDI parent form is the form that contains all the MDI child forms. In .NET, an MDI parent form can have multiple MDI child forms, but it cannot be a MDI child form to another MDI parent form simultaneously. In other words you cannot have the same form to be an MDI parent and an MDI child at the same time. The MDI child forms are displayed as independent windows under an MDI parent window. A user interacts with an MDI application by using the MDI child windows and the MDI parent window. Thus, in .NET you can integrate multiple windows in an MDI application. An MDI parent form can be created either at design time or at run time by setting the IsMdiContainer property of the form to true.

Since an MDI application can have multiple windows or documents, you need to provide a mechanism to switch between the windows or documents. To enable users to switch between multiple windows or documents, you can create menus for an MDI application.

Practical Demonstration

Problem Statement:

Create a data entry application. This application should enable users to access multiple data entry forms. To allow easy access to different data entry forms, the data entry application should provide a user friendly interface. It should also allow users to access the monthly sales report for data analysis. In addition, the users should be able to exit the data entry application when required. The forms that the users should be able to access are Customer Details form, Employee Details Form, and Order Details form.


The following are the steps for the same

1. Design the required integration.
2. Integrate the application
3. Save and execute the application.

I. Design the Required Integration

The menus can be organized as given below:

II. Integrate the application

1. In the form property, set true for IsMdiContainer property. This will set the form as the MDI parent form.

2. Secondly, set the WindowState property of form to Maximized. Since this form is the MDI parent form within which all the MDI child forms are to be displayed, so display it as maximized.

3. Double click the MainMenu control in the Toolbox to add the control to the component tray.

4. Add the menu items as planned. To add the menu items click the "Type Here" box, and type the menu item name.

5. This is how the form should like. You can specify the name i.e. the text visible in the form either by directly writing on the form or going to the properties and changing the text property.

6. You can add child forms by adding item through the solution explorer. And write the following code in the click event of Customer Details submenu item.

Private Sub CustomerDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerDetails.Click
    Dim NewMDIChild As New frmCustomerdetails   ' where
    ' frmcustomerdetails is the child form to be invoked
    NewMDIChild.MdiParent = Me
End Sub

Using LayoutMDI method

We can use the LayoutMDI method with the MDILayout enumeration to rearrange the child forms in an MDI parent form.

The MDILayout enumeration can have four values, which will display child forms as cascading, horizontal or vertical. These methods are often used as the event handlers called by a menu item's Click event. In this way, a menu item with the text "Cascade Windows" can have the desired effect on the MDI child windows.

Context Menu Example

To enable context menus in your .NET Windows Forms application, use ContextMenuStrip control. Drag one ContextMenuStrip control from toolbox to new empty windows form. Now, place one TextBox or some other standard windows control. Standard controls have a property ContextMenuStrip. Set value of this property to previously added ContextMenuStrip control (default name is ContextMenuStrip1). With this property you can specify context menu for every control. Download sample Context Menu project, which use two RichTextBox controls and enables Cut, Copy and Paste operations by using context menus. This is how the program should look like:

You can run application, select and cut or copy some text in one RichTextBox, and paste it to another RichTextBox by using context menu facility.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   

comments powered by Disqus