Working With Menus
Adding Menus
VB makes it easy to add professional looking menus to your applications.
Most menu creation functions are taken care of by the Menu Editor, located on the toolbar.
After you have added a menu to your form you can use event procedures to process the menu commands.
Creating a Simple Menu
Create a new Standard EXE project
Click the Menu Editor button on the toolbar.
You need to specify two fields in a Menu:
The Name--what name it will have in the program code
The Caption--the name it will have on screen.
There are additional settings, Index, HelpContextID, Shortcut, and Checked, which we will talk about later.
The Clock Program
Type Time in the Caption box of the Menu Editor.
Next Type mnuClock in the Name box.
We use mnu to identify a menu item in the program for coding purposes. We will always use mnu to identify these commands.
Click the Next button to add this to your menu.
The Clock Program cont...
Next add the Date to our Menu as our program will also display the current date.
Type Date in the Caption box and type mnuDatemItem in the name box.
We will make date a command rather than a menu title, so we add the word Item to the end of the name. This will help us distinguish between menu command and menu titles in the Code window.
The Clock Program cont...
With the Date item highlighted press the right arrow button to indent it.
The position of an item in the list box determines if it is a menu title (like our clock item) or a menu command (our date item).
Click the next button and add Time and the menu item mnuTimeItem.
The Clock Program cont...
Click O.K. to close the Menu Editor.
You will now see the menu items you just added. Notice that they are active even though you have not started the program.
By clicking an item in the menu you will activate the event procedure that is activated when a user selects that menu item.
Before we write those procedures lets add the keyboard support, which is found on most menus.
Adding Keyboard Keys
It is quite easy to assign keyboard shortcuts to menu items in VB.
All we need to do is to add an "&" in front of the letter that we wish to make the access key.
For example: to make the C in clock active all we need to do is to place an & in front of the letter C in our menu.
Changing the Order of Menu Items
To change the order of items in a menu all we need do is to open the menu editor and click the up or down arrows to move one of our items in the list.
Lets move the Time item up one level.
Thats it, we have created our menu!
Lets now create the display box and write the code.
Display box
Lets add a label box to our form to display our date and time.
Create a small label box in the top middle of the form and set the Alignment property to Center, the Border Style to Fixed Single, the Caption to (none) and choose a nice bold font.
Write the Code
Next we need to write the event procedure for the Time item.
Double click your forma nd select the mnuTimeItem from the Object drop down box.
The mnuTimeItem_Click() appears.
Press the spacebar 4 times and write:
Label1.Caption=Time
Write the Code
This statement inserts the Time from the computer's system clock and will display it in the Label1 object.
Next Click the mnuDateItem_Click item.
Type 4 spaces and insert the code Label1.Caption=Date
Thats the basics lets add some other objects!
Menu Conventions
It is important to be consistent in creating menus.
Use short and specific captions of one or two words.
Assign each menu item a unique access key.
If the command has an off or on state, place a check mark next to it.
Place ellipses ( ) after a menu command that requires the user to enter more information.
Use menu naming conventions such as mnu and Item in your applications and be consistent.