Greeter
Now check out your new menu!
- Open the Menu Editor window.
- Press [ENTER] or click the Next button. The cursor appears in the Caption text box, and you can create a new menu option.
- Type "&Background" (without the quotes) for the caption.
- Press [TAB] and then type mnuOptBackground in the Name text box.
- Click the Menu Editor's right-arrow button to indent Background in the Menu outline. This will make Background a part of the Options menu.
- Click the Next button to create a new menu option.
- Type "Light &Blue" (without the quotes) for the caption and "mnuOptBackgroundBlue" (without the quotes) for the name.
- Click the right-arrow button to indent this option one level more than the indentation level for Background.
- Create two more options at this menu level. Type "Light &Gray" and the name "mnuOptBackgroundGray" for the first caption. Type "&White" and the name "mnuOptBackgroundWhite" for the second caption.
- Create another option by typing "&Style" for the caption and "mnuOptStyle" for the name.
- Click the left-arrow button, and press [ENTER] to move this menu option to the Background indentation level.
- Type "&Plain" for the caption and "mnuOptStylePlain" for the name. Click the right-arrow button, and press [ENTER] to make this option a cascading menu option under Style.
- Type "&Modern" for the caption and "mnuOptStyleModern" for the name. Press [ENTER].
- Type "Bor&der" for the caption and "mnuOptBorder" for the name. To move this option to the same level as Background and Style, click the left-arrow button.
- Close the Menu Editor window by clicking on the OK button.
Now we'll look at the new menu, from a running application.
- Press F5 to run the application.
- Select the Options menu.
- Select Background to open a cascading menu that displays the three color options.
- Select the color you want.
The menu should be arranged as follows:
- Options
- Background
- Light Blue
- Light Gray
- White
- Style
- Border
Note that nothing happens when you select that color. You've designed the look of the menu system, but you must add code to make each menu option operate the way you want.
- Stop the application.
- Open the Menu Editor window.
- Click &White in the Menu outline to select it.
- Click the up-arrow button two times. Notice how &White moves up the outline to a position above "Light &Blue." Note that the down-arrow button moves menu options down the list, as expected.
You can delete menu items:
- Click the Border menu option in the Menu outline.
- Click the Delete button.
Now we'll add a new menu.
- In the Menu Editor window, click &Options in the Menu outline.
- Click the Insert button three times to insert three spaces in the Menu outline.
- Position the cursor in the Caption text box.
- Type "&Message" for the caption and "mnuMessage" for the name, then press [ENTER] to create the menu.
- Type "&Text" for the caption and "mnuMessageText" for the name of the second menu option.
- Click the right-arrow button, then press [ENTER] to make this item a submenu option under the Message menu.
- Type "&Greeting" for the caption and "mnuMessageGreeting" for the name to create the third menu option.
- Click the right-arrow button to move this option to the same indentation level as &Text.
- Close the Menu Editor window by clicking on the OK button.
Now navigate through the menus and see what you've got. Let's add some special effects, shall we?
- Open the Menu Editor window.
- In the Menu outline, select the &Greeting menu option.
- Insert some space for the separator bar by clicking the Insert button.
- Position the cursor in the Caption text box and type a hyphen (-) for the caption and mnuMessageSepBar1 for the name; then press [ENTER].
- In the Menu outline, click the &Text menu option
- Click the Caption text box, and press [END] to move the cursor to the end of the caption. Type three periods to create an ellipsis (...). The caption is now "&Text..."
- Close the Menu Editor window by clicking on the OK button.
Look over your menus again. Now let's assign some shortcut keys, make some of the menu items check-able, and disable a menu option.
- Open the Menu Editor window.
- In the Menu outline, click the "&Text..." menu option.
- Click the Shortcut list box (or the list arrow) to pull down the list of possible shortcut keys.
- Use the scroll bar to find "Ctrl+T" and click on it. Now the shorcut key CTRL-T is associated with the "&Text..." menu option.
- In the Menu outline, click the &White menu option.
- Select the box labelled Checked by clicking it with the mouse.
- Click the &Plain menu option in the Menu Outline, and click the box labelled Checked. &White and &Plain are now check-able.
- Click the &Modern menu option in the Menu outline.
- Click the Enabled check box to deselect it; the check mark will disappear. &Modern is now disabled.
- Close the Menu Editor window by clicking on the OK button.
Run the program (hit F5 or press the VCR Play button) and test the menu selections. Now we'll add the code to make the menus actually work.
- Double-click the form (NOT a control). The Code window should appear.
- Click the Object drop-down menu, find mnuOptBackgroundBlue, and click on it. The "Click" code for this menu should appear (it'll be called Private Sub mnuOptBackgroundBlue_Click () ).
- Type in the folowing, between the lines starting with "Private Sub" and "End Sub:"
Form1.BackColor = &HFFFFC0
mnuOptBackgroundBlue.Checked = True
mnuOptBackgroundGray.Checked = False
mnuOptBackgroundWhite.Checked = False
If mnuOptStyleModern.Enabled Then mnuOptStylePlain_Click
mnuOptStyleModern.Enabled = False
|
- Click the Object drop-down menu, find mnuOptBackgroundGray, and click on it. Type in the following between the "Private Sub" and "End Sub" lines:
Form1.BackColor = &HE0E0E0
mnuOptBackgroundBlue.Checked = False
mnuOptBackgroundGray.Checked = True
mnuOptBackgroundWhite.Checked = False
If mnuOptStyleModern.Enabled Then mnuOptStylePlain_Click
mnuOptStyleModern.Enabled = False
|
- Click the Object drop-down menu, find mnuOptBackgroundWhite, and click on it. Type in the following between the "Private Sub" and "End Sub" lines:
Form1.BackColor = &HFFFFFF
mnuOptBackgroundBlue.Checked = False
mnuOptBackgroundGray.Checked = False
mnuOptBackgroundWhite.Checked = True
mnuOptStyleModern.Enabled = True
|
- Now find mnuOptStylePlain and enter the following:
mnuOptStylePlain.Checked = True
mnuOptStyleModern.Checked = False
lblSalute.ForeColor = vbBlue
lblSalute.Font = "Terminal"
lblPerson.ForeColor = vbBlue
lblPerson.Font = "Terminal"
|
- Find mnuOptStyleModern and enter the following:
mnuOptStylePlain.Checked = False
mnuOptStyleModern.Checked = True
lblSalute.ForeColor = vbRed
lblSalute.Font = "Modern"
lblPerson.ForeColor = vbRed
lblPerson.Font = "Modern"
|
- Find mnuMessageText and enter the following:
Dim Msg$, Title$, DefaultValue$
Msg$ = "Enter the name of the person you want to greet."
Title$ = "Name Please"
DefaultValue$ = "You"
lblPerson.Caption = InputBox(Msg$, Title$, DefaultValue$)
|
- Find mnuMessageGreeting and enter the following:
mnuMessageGreeting.Checked = Not mnuMessageGreeting.Checked
IF mnuMessageGreeting.Checked = True Then
lblSalute.Caption = "Welcome to"
Else
lblSalute.Caption = ""
End If
|
- Open the Menu Editor window, click &Greeting in the outline, click the box labelled Checked, and click OK.
Now run the application, select Text from the Message menu, type in your name, and choose OK. Fiddle around with the menus and familiarize yourself with how they work, then look back at the code and try to see how the code makes the menus do what they do.
Visual Basic by Example is hosted by GeoCities