VB6 Tutorial 59: Menus

By | 2018-06-01

Menus are one of the most important features of a software product. Every standard software must have menus. You generally see a menu on top of a software interface.

Menus are controls but different from the controls in the ToolBox and they don’t work like the other controls. You can drop a menu on the form from the Menu Editor Window. Press Ctrl+E to show the Menu Editor Window or right-click on the form and click Menu Editor. The Menu Editor Window can also be shown from the Menu Editor icon of the ToolBar.

Building a menu

Building a menu is very simple, you can do it on your own. Simply fill the Caption and Name field in the Menu Editor Window and click ok to create it.

Click the right-arrow button to create a submenu. Click Next to create the next menu item, and click ok once you’re done editing the menu items.

A simple form with menu:

Properties of the menu items

The important properties of the menu items are Name, Caption, Checked, Enabled, Shortcut and Visible. As per your programming need, set the properties either in run-time or in design time. You can create a shortcut key for a menu item. In some situations you may want to disable a menu item, you can acquire it through the Enabled property.

The menu control exposes only one event, the Click event.

Example

Make a menu as same as the following image.

Now write the following code.

Private Sub mnuBlue_Click()
    Form1.BackColor = vbBlue 'Makes the Form blue
End Sub

Private Sub mnuGreen_Click()
    Form1.BackColor = vbGreen 'Makes the form green
End Sub

Private Sub mnuRed_Click()
    Form1.BackColor = vbRed  'Makes the form red
End Sub

Private Sub mnuWhite_Click()
    Form1.BackColor = vbWhite  'Makes the form white
End Sub

Now run the program and Click on the menu items and see what happens.

The Checked property

Design a form like the output image of the following program. Create a Help menu and drop a Label control on the form with the caption ‘Help’.

Now write the following code.

Private Sub mnuShowHelp_Click()
    If mnuShowHelp.Checked = True Then
        mnuShowHelp.Checked = False
        Label2.Visible = False
    ElseIf mnuShowHelp.Checked = False Then
        mnuShowHelp.Checked = True
        Label2.Visible = True
    End If
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.