VB Lecture Notes - Menus
Objective #1: Create menus.
- Professional-looking menus can be added to a VB project using the MenuStrip tool.
- To add a menu to a form, double-click the MenuStrip tool in the toolbox. The MenuStrip object will appear in a pane below your form's designer window. Rename the MenuStrip tool object
using the prefix "ms". 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. Rename the menu command mnuFile using
the proper menu command prefix mnu. Type
"E&xit" in the area marked "Type Here" that is below the File menu command that you just created. Name this menu command mnuFileExit. It is
proper style to name a menu command object after its "parent" menu command. 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.
- The Exit menu command should 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