MENUS, STANDARD CODE MODULES
MENUS
Menus consist of a menu bar with menu names,
each of which drops down to display a list of menu commands.
To
use menu commands to activate a procedure.
Defining
Menus
To
use the Menu Editor window in VB to set up your menus.
Figure
shows the Menu Editor window with the various parts labeled.
The Caption
The
Caption property holds the words you want to appear on the screen (just like
the Caption property of a label or command button).
Use
the ampersand (&) in the Caption to specify the key to use for keyboard
access.
For
example, for File, the Caption should be &File.
i.e.,
Press Alt+F, to open the File menu.
The
Name
The
Name box indicates the name of the menu control, similar to other
controls.
The
Name is required.
Naming
Standards
The
three-character prefix for a menu name is “mnu”.
Therefore,
the name for the File menu should be mnuFile.
Submenus
The
drop-down list of commands below a menu name is called a menu.
When
a command on the menu has another list of commands that pops up, the new list
is called a submenu.
A
filled triangle to the right of the command indicates that a menu command has a
submenu.
To
specify menu names, menu commands and submenu commands by their indentation
levels.
Menu
names appear at the left of the list box; for a menu command, click on the
Right arrow button to indent one level.
For
submenus, click on the Right arrow button again to indent two levels.
The
indentations show as dots (….) in the list box.
The
Menu List Box
The
menu list box contains a list of the menu items you have created and shows
their indentation levels.
You
can move an item up or down, left or right by clicking on its name in the list
box and clicking on one of the arrow buttons.
&File
….E&xit
&Help
….&About
Creating
a Menu--Step-by-Step
You
are going to create a project with one form and a menu bar that contains these
menu items:
File Help
Exit About
STEP
1: Display
the Menu Editor window by selecting Menu Editor from the Tools
menu or clicking on the Menu Editor from the Toolbar
button.
STEP2:
Type
the Caption
(&File) and Name (mnuFile) for the first menu.
STEP
3: Click
on the Next button or press Enter;
the text boxes will clear and the name of your first menu appears in the menu
list box.
STEP
4: Click
on the Right arrow button, which sets the indentation level for a menu
command.
STEP
5: Click
in the Caption text box to set the focus and then type the Caption and the Name
for the Exit menu command.
STEP
6: Click
on the Left arrow (outdent) button to return to the previous level.
STEP
7:
Repeat the steps to create the Help menu and the About command
indented below it.
STEP8:
Click
OK when you are finished. The new menu will appear on your form.
Coding
for Menu Commands
After
create form’s menu bar, it will appear on the form in design time.
Just
select any menu command, in the Code window where write the code.
For
example, in design time, open your form’s File menu and choose Exit.
The
Code window will open with the mnuFileExit_Click procedure displayed (assuming
you have followed the suggested naming conventions and named the Exit command
mnuFileExit).

STEP 1: Code the procedure for the Exit by pulling down the menu and clicking on the word Exit. Type the End statement.
STEP
2: Use
a MsgBox statement in the procedure for the Click event of the About on
the Help menu. The Message string
should say “Programmed by” followed by your name.
Modifying
a Menu
In
design time, to select any menu item in the list box and its information will
appear in the top of the window where you can make modifications if
desired.
To
click on the Delete button to delete the selected item or click Insert to
insert a new blank line before the selection.
To
change the indentation level of any item, first select it and then click on the
Left or Right arrow button.
To
move an item up or down in the list by first selecting it and then clicking on
the Up or Down arrow button.
Checked
and Enabled
A
menu command may contain a check mark beside it (checked), or it may be
grayed (disabled).
STANDARD CODE MODULES
If
the procedure will be used in only one form, then it should be included in the
code for that form module. If, in fact,
you will need to use the procedure in multiple forms, write the procedure in a standard
code module. A standard code
module is a Basic file with the extension .bas, which is added to
the project. Standard code modules do
not contain a form, only code.
Create
a new standard code module by selecting the Add Module command on the Project menu.
Select Module on
the New tab of the Add
Module dialog box and click on Open. A new Code window titled Module1 opens on
the screen. Also note that Module1 is
added to your Project Explorer window.
Make sure that Module1 is highlighted in the Project Explorer window and
choose Save Module1 from the File menu to give the new file a name. It is a good idea to name this file the same
as your project file unless you plan to use it for multiple projects. The file extension is .bas by default.
A
standard code module has a General Declarations section and
procedures, just like a form module.
Form
the Add Module menu option, you may also select an existing standard
code module. This technique allows you
to use a code module in more than one project.
Comments
Post a Comment