VB Lecture Notes - Menus
Objective #1: Create menus.
- Professional-looking menus can be added to a VB project using the MenuStrip tool.
- Follow these steps to add a menu to a project like you will need to do in the Maze1 project. The menu will look like this
File Mouse
Exit Go
Reset
1.
Double-click the MenuStrip tool in the toolbox. The MenuStrip object will appear in a pane below the form's designer window. You will also see it appear across the top edge of your form.
2.
Rename the MenuStrip tool object
using the prefix ms. A good name would be msMain since this will be the main menu in your program.
3.
Type
"&File" in the area that's marked "Type Here". This creates a menu command object that will appear on the top of the menu as File. The & precedes the letter that will be underlined and access as a shortcut, access key.
4. Type
"E&xit" in the area marked "Type Here" that is below and a little to the right of the File menu command that you just created.
5. Type "&Mouse" in the area marked "Type Here" that is to the right of the File menu command.
6. Type "&Go" in the area marked "Type Here" that is just below the Mouse menu command.
7. Type "&Reset" in the area marked "Type Here" that is just below the Go menu command.
8.
Single-click the File menu command and change it's Name property to mnuFile The proper prefix for menu commands is mnu. Don't confuse this with ms which is proper prefix for the whole MenuStrip.
9. Single-click the Exit menu command and rename it as mnuFileExit. It is
proper style to name a menu command object after its "parent" menu command. In this example, File is considered to be a parent menu command since it is at the top of the menu. Exit is a child menu command since it drops down from the File menu command. File is considered to be the parent and Exit is the child.
If you create a submenu command by typing in the area to the right of a child menu command, you should name it
after both its parent command and its parent's parent command name. For example, the Drawing toolbar menu command in Microsoft Word should be named mnuViewToolbarsDrawing because View is the parent and Toolbars is a child that drops down from View and Drawing is a submenu command that juts out from Toolbars.
10. Single-click the Mouse menu command and name it mnuMouse.
11. Single-click the Go menu command and name it mnuMouseGo since the parent of the Go menu command is the Mouse menu command.
12. Single-click the Reset menu command and name it mnuMouseReset since the parent of the Reset menu command is the Mouse menu command and not the Go menu command.
- An Exit menu command should always be at the very bottom of the File menu command according to standard Windows conventions.
- You can add a separator bar to a menu to designate groups of unrelated menu commands under the same parent command. Right-click a menu command and click Insert Separator.
- To remove an unwanted menu command or separator, select it and right-click to click the Delete command.
- If your menu doesn't show up at the top of the form, make sure that the form's MainMenuStrip property is set to the name of the menu.
Objective #2: Use the Checked & Enabled properties with menus.
- The Checked and Enabled properties of menu commands can be used to give feedback to the user and enhance the interface
of your program. You should always use these properties with menu items when appropriate. Users appreciate it when menu commands are enabled only when they are appropriate choices. If you notice, most
commercial Windows applications follow this convention.
- Often, limiting the user's to choosing appropriate commands prevents the programmer from having to test for illegal user inputs. Leaving clues and even error messages for the user
explaining why certain commands are indeed inappropriate provides a better experience for the user. Things such as checkmarks placed next to previously selected menu items enhances the user interface
of a program as well.
- If a menu command object's Checked property is set to True, then a checkmark will appear to the left of the menu item. If it is set
to False (which is the default), then a checkmark does not appear. For example
mnuToppingsCaramel.Checked = True
- If a menu command object's Enabled property is set to True (the default), then the menu command can be chosen by the user. If it is
set to False, then the menu command will be "grayed out" so that the user knows that he cannot choose that option. For example
mnuMouseSpeedSlow.Enabled = False