Menus
Up
Almost all serious computer programs have menus, so let's learn how to add a menu bar to our programs. VB makes it easy! The program designed here is designed for simplicity - it doesn't do much except demonstrate how to add menus to your program.

Make a new Standard Exe project and add a label called lblSample as shown:

The label caption has been centered and set to size 14.

Now open the Menu Editor in the Tools menu. Create the File menu heading by typing the caption and name as shown below - don't click the OK button yet. The ampersand (&) in front of the heading creates a shortcut to the menu - the user will be able to access this menu by typing Alt-F. Note that menu item names have the 3 letter prefix 'mnu'.

Click Next and create the first item of the File menu by typing the caption &Reset and the name mnuFileReset. Note how the name indicates that the Reset item is part of the File menu. In order for the Reset item to be part of the File menu, select &Reset from the lower panel of the menu editor window, then indent it by clicking the right arrow. The window should now look like this:

Add more menu items as shown here (the File and Reset items do not appear due to lack of space):

Note how the shortcut letter for the exit item is X, not E, and note that the Random item could not use the letter R for its shortcut since this was already being used by the Red item in the same menu. For the Exit item, an additional shortcut key combination, Ctrl+X, was added by using the Shortcut pull-down menu in the menu editor window. Between the Green and Random items an item has been added with the caption '-' (just a hyphen) - this item will cause a separator line to be displayed between the Green and Random items. When you have finished setting up the menus as shown, you can at last click the OK button.

You can try running the program and all the menus should appear as expected but none of the menu items do anything yet, since we have not yet written code to handle the menu events. Let's start with an easy one: the Exit item.

In design mode, not run mode (just stop the program, stupid) choose the Exit item and the code window will open with the necessary wrapper lines to handle the mnuFileExit_Click event. Add the word 'End' so that the code looks like this:

Private Sub mnuFileExit_Click()
End
End Sub

Run the program and check that the Exit item works. Try also typing Alt+F, followed by X, as well as simply Ctrl+X.

Now copy and paste the code below to set up all the other menu items:

Private Sub Form_Load()
Randomize
End Sub

Private Sub mnuFileExit_Click()
End
End Sub

Private Sub mnuBackgroundRed_Click()
Form1.BackColor = RGB(255, 0, 0)
End Sub
Private Sub mnuBackgroundGreen_Click()
Form1.BackColor = RGB(0, 255, 0)
End Sub

Private Sub mnuBackgroundRandom_Click()
Form1.BackColor = Rnd() * 256 * 256 * 256
End Sub

Private Sub mnuTextBold_Click()
' switch between checked and not checked
mnuTextBold.Checked = Not mnuTextBold.Checked
lblSample.Font.Bold = mnuTextBold.Checked
End Sub

Private Sub mnuTextItalic_Click()
mnuTextItalic.Checked = Not mnuTextItalic.Checked
lblSample.Font.Italic = mnuTextItalic.Checked
End Sub

Private Sub mnuHelpAbout_Click()
MsgBox ("Copyright Nigel Ward, April 2001")
End Sub

I'll leave you to add a procedure to handle the mnuFileReset event. This should set the text back to normal (not bold nor italic) and should set the form's backcolor property to its original gray color which is given by RGB(210, 210, 210). It should also set the checked property of the two background colour menu items to false.

 Some points to note:

bullet

The Randomize command in the Form_Load procedure must always be included in any program that uses the random number generator, rnd(). The random number generator is explained in other lessons.

bullet

The RGB() function allows the red, green and blue components of colors to be specified in sequence, with each component in the range 0-255. The function then converts the sequence into the single number needed for the BackColor value. Setting the BackColor to RGB(255,0,0), for example, would set the background to bright red, and setting it to RGB(255,255,255) would make the background white (bright red + bright green + bright blue = white). This gives more control over the color than using color constants such as BackColor = vbred.

bullet

It's sometimes useful to make the items of a given menu into a 'control array' as explained in other lessons. In this example, the items in the Background menu could have been made into a control array by giving them all the name mnuBackgroundColor, for example. Since members of a control array share the same code, this may have made the code shorter.

bullet

It's possible to make a menu that consists of a heading only. Then the code is activated when the user clicks on the heading. While this is simple, it's bad practice because users do not expect menus to behave in this way so they may think something is wrong.

To finish learning about menus, try changing the Background menu as shown below, and see what happens:

Note that some of the items are now double-indented. Strictly speaking you should now change the names of these items to show all the levels above them, such as mnuBackgroundColorRed, but if you change the names you will also need to change the code to match the new names. Even if you do not change the names, the program should still work fine.

Previous Up Next