Programming Fundamentals of Visual Basic 6.0
Programming Fundamentals of Visual Basic 6.0
of
Visual Basic 6.0
Level 1
Table of Contents
If...Then...[Else...]End If ...........................................................7-26
If...Then...Else[If]...End If .........................................................7-27
Select Case .............................................................................7-28
GoTo .......................................................................................7-29
Looping Structures ..............................................................................7-31
What Is a Loop?.......................................................................7-31
For...Next.................................................................................7-31
DoLoop ................................................................................7-33
Using For...Next.......................................................................7-35
A DoLoop Example ..............................................................7-36
INDEX......................................................................................................INDEX-1
Creating and
Using Menus
Objectives
Review the basic elements of menus in Windows applications.
Understand how Visual Basic implements menus.
Learn how to use the Visual Basic Menu Editor.
See how to run VBA code from a menu command.
See how to change menu properties at run time.
Use menu templates.
Learn how to create popup menus for your forms.
Menu Basics
Figure 1 shows a diagram illustrating the basic aspects of menus in Windows
applications. You can see these menus on the sample form for this section. All
of these terms are summarized below.
Menu Bar
Top-Level
Menu
Command
Checked
Menu Item
Disabled
Menu Item
Access Key
Drop-Down Fly-Out
Menu (or Cascading)
Menu
Windows has a menu bar that appears just below the window caption
and contains all the top-level menu commands.
Clicking on one of the top-level menu commands reveals a drop-
down menu containing other commands.
Drop-down menus may have fly-out or cascading menus that appear
to the left or right of a menu command when the user selects it. These
contain other menu commands and can even contain other fly-out
menus!
Menu separators are used to segregate groups of related menu items
or to provide a visual break in a large menu structure.
Menu commands usually contain access keys denoted by underlined
letters in the menu text. Users can quickly select menu commands by
pressing the ALT key along with the access key.
Some commonly used menu commands may also have a shortcut key
(usually a function key or a letter combined with the Ctrl, Alt and/or
Shift keys). Shortcut keys activate the menu command without having
to select it.
Menu commands can be disabled, preventing the user from selecting
them.
Menu commands can also be checked, indicating a persistent option
that can be toggled by selecting the menu.
Figure 2 shows the Menu Editor window. You can only open the Menu Editor
when a form designer window is active. Select Tools|Menu Editor, press
The Menu Editor CTRL+E, or click the Menu Editor toolbar button (shown on the left). The
button Menu Editor modifies the menu of the active form.
Figure 2. You create menus for each form using the Menu Editor.
1. Enter a caption for the menu command in the Caption field. (Include an
ampersand (&) in the caption to create an access key.)
NOTE The name you enter in the Name field is the control name youll
use in any VBA code you write.
Visual Basic will add the new menu command to the list of commands at the
bottom of the dialog box. Figure 3 shows the Menu Editor after creating a new
menu command named mnuFile with the caption &File.
To create another new command, press ENTER to select the next, blank menu
command in the list. You can also click the Next button.
To insert new menu items in the middle of the list, select the item before which
you want to insert the new item and click the Insert button. (The Menu Editor
always maintains a blank row at the end of the list for adding new menu
commands at the end.)
Likewise, you can click the Delete button to remove a menu item.
TIP: You create menu separators by setting the Caption property of a menu
command to a hyphen (-). Even though users can never select a menu
separator you must still assign it a unique Name property.
The Shortcut property defines the shortcut key for the menu
command. You choose from a set list of keyboard combinations (e.g.,
CTRL+C, SHIFT+F9). No two menu items can have the same shortcut
key.
The HelpContextID property lets you assign a Help topic to the menu
command. (The Help file itself is set by the applications properties.)
The Checked property allows you to place a check mark next to the
menu command at design time.
The Enabled property allows you to enable or disable a menu
command. Users can see, but not select disabled menu items.
The Visible property allows you to hide a menu command at design
time. You can set this property to True at run time to display the
command.
The WindowList property designates the menu as a window list.
Window lists are used in MDI applications to display a list of the open
child windows.
The Index property allows you to create arrays of menu items. Each
item in the array has the same name, but is differentiated by its index.
This allows you to write one event procedure for the whole group of
items. This feature also allows you to create menu items at run time
(using the Load statement, not covered here).
You change the hierarchical structure using the arrow buttons on the Menu
Editor dialog box. Indenting a menu command once to the right creates a drop-
down menu. Indenting a menu command twice to the right creates a fly-out
menu.
TIP: You can change the position of a menu command by clicking the up or down
arrows.
Figure 5 shows the Menu Editor open to the File menu of the sample form.
Menu Events
Menu commands have one event, Click, that is triggered when a user selects
the command. You write VBA code to respond to a menu selection in the
Click event. The sample form has numerous menus that display a message box
when you click them. The code for one of those is shown below:
Changing menu properties at run time is simple because, after all, menus are
just controls. The sample form features buttons that toggle the Enabled and
Visible properties of various menu items. The code listing below contains the
event code from the buttons.
TIP: Although there are other ways to toggle the state of a True/False (Boolean)
property value, the simplest way is to use the Not operator. This operator
toggles a Boolean value so that if it was True, it becomes False. If the value
was False, it becomes True.
You can also change a menus Checked property to add or remove a check box
next to the menus caption. This has been done for the Tools|AutoCorrect
menu on the sample form. Code in the menus Click event accomplishes the
task:
End Sub
TIP: You can also change a menus Caption property. This lets you change the
menu text as necessary.
Reusing Menus
Visual Basic 6.0 makes it easy to set up a forms menu structure, and then
reuse it later on other forms. By setting up menu templates for commonly used
menu structures, you can save a great deal of time when designing forms.
To install the Template Manager, follow these steps (the result of the steps is
shown in Figure 6):
3. Click the Loaded/Unloaded check box (to load the add-in immediately)
and the Load on Startup check box (so the add-in loads for future Visual
Basic sessions).
Figure 6. Add the template manager add-in, so you can add existing menu
structures to your forms.
With the form youd like to modify selected, choose the Tools|Add Menu
menu item, and youll see the dialog box shown in Figure 7. Select the menu
structure youd like to add to your form, and Visual Basic does the rest for
you. It reads the menu information from the selected template, and adds the
menus supplied by the template to your form. In addition, the Add Menu tool
also copies any code associated with the menu items to your form, as well.
NOTE If any existing top-level menus in your form conflict with any top-
level menus in the template, Visual Basic will add no new menus
to your form. Youll get no indication of whats gone wrong, but
nothing on your form will change.
Try It Out!
Follow these steps to try out the menu templates:
3. Choose the Explorer File Menu item from the list of templates, and click
the Open button.
5. Double-click on the form to view the forms module. Note all the code that
was placed in your form when Visual Basic added menus to the form.
To create a menu template, save your template form (with its menus created,
and associated code written) to the Templates\Menus folder underneath the
Visual Basic folder. This location is user-settable, however, and may have
been changed. To get the correct folder, use the Tools|Options menu item, and
check the Environment page, as shown in Figure 8.
Figure 8. Check on this page to make sure you place your menu template in the
correct folder.
Try It Out!
To test the menu template mechanism, follow these steps:
2. Right-click on the form, and select the Save Menus.frm As... item.
3. In the Save File As dialog box, navigate to the folder shown in the
Templates Directory field in Figure 8, and drill down into the Menus
subfolder.
4. Change the name to AppDevTest, and click the Save button to save the
new copy of the form.
6. Select Tools|Add Menu, and choose the new AppDevTest item you find
there.
7. Your form should now have menus identical to the original forms menus,
including all the code that was in the forms module.
8. When you quit Visual Basic, make sure you dont save the project or any
of its items, because saving the form to a new folder changed the project
file, and you may want to work with the project again later.
Figure 9. The sample form features a popup menu that appears when you click the
right mouse button.
Figure 10 shows the Menu Editor open to the mnuPopup menu, which is the
top-level menu for the popup menu. Note that the Visible check box is
unchecked. Since the user will never see the top-level menu it doesnt matter
what the caption is.
Figure 10. You can create a popup menu on a form by hiding the top-level item.
For example, the following line of code would create a popup menu from the
mnuPopup menu and display it when the user clicked the right mouse button
on the form:
TIP: You can control the location where the popup menu appears by passing x and
y coordinates to the PopupMenu method.
Summary
Each form in a Visual Basic application can have its own custom
menu.
Use the Menu Editor to create and modify form menus.
Visual Basic supports top-level, drop-down, and fly-out menus.
Visual Basic treats each separate menu item as a control, complete
with an event procedure (Click).
Change menu item properties at run time.
Visual Basic 6.0 provides a simple way to reuse menussave a form
with the menus you like to the Templates\Menus folder, and then
choose the Tools|Add Menu menu item.
Create popup menus by calling a forms PopupMenu method.
Questions
1. How do you specify the hierarchical structure of a menu system?
3. How do you run the code associated with a menu item without selecting it?
Answers
1. How do you specify the hierarchical structure of a menu system?
Use the arrow keys in the menu editor to indent or outdent menu
items
3. How do you run the code associated with a menu item without selecting it?
Assign the menu command a shortcut key and press it
Lab 5:
Creating and
Using Menus
Lab 5 Overview
In this lab youll learn how to create and manipulate menus on Visual Basic
forms.
Objective
In this exercises youll use the Visual Basic Menu Editor to create a simple
menu structure. Youll begin with a blank form and create a top-level menu
bar consisting of File and Help menus. Beneath the File menu youll create
New, Open, Save, and Exit menu commands, with a menu separator between
Save and Exit. Beneath the Help menu youll create an About menu. Lastly,
youll assign shortcut keys to the New, Open, Save, and Exit commands of
Ctrl+N, Ctrl+O, Ctrl+S, and Ctrl+Q, respectively.
Things to Consider
How do you open the Menu Editor?
How do you add menus and menu commands?
How do you designate top-level menus and menu commands beneath
them?
How to assign a shortcut key combination to a menu command?
Step-by-Step Instructions
1. Begin by launching Visual Basic and creating a new Standard EXE
project. Visual Basic presents you with a blank form, Form1.
2. Open the Menu Editor by clicking the Menu Editor toolbar button or by
selecting Tools|Menu Editor (or press CTRL+E). Visual Basic opens the
Menu Editor dialog box (see Figure 11).
Figure 11. The Menu Editor dialog box showing no menu items.
3. Create the File menu by typing &File in the Caption field and mnuFile in
the Name field. Visual Basic adds the text &File to the first row in the
list at the bottom of the dialog box.
4. Press ENTER or ALT+N, or click the Next button to add the next menu
command. Visual Basic highlights the second row in the list and places the
insertion point back in the Caption field.
5. Create the New menu command by typing &New in the Caption field and
mnuFileNew in the Name field.
6. From the Shortcut list, select CTRL+N as the shortcut key combination.
8. Repeat steps 5 through 7 to create the menu commands listed in the table
below:
9. Select the &New menu command in the list at the bottom of the dialog box
and click the right arrow button once. Visual Basic adds four dots in front
of the menu command, indicating that it will appear beneath the File menu.
10. Repeat this step for each of the remaining menu commands that will
appear beneath the File menu.
11. When you have finished indenting the existing menu commands, click on
the blank row in the list of menu commands, just beneath the E&xit
command.
12. Create the Help menu by typing &Help in the Caption field and mnuHelp
in the Name field. Press ENTER. Visual Basic highlights the next blank
row.
13. Create the About menu command by typing &About in the Caption field
and mnuHelpAbout in the Name field.
14. Indent the &About menu command beneath the Help menu by selecting it
in the list and clicking the right arrow once. The Menu Editor should now
display the menu structure shown in Figure 12.
Figure 12. You should have a menu structure that looks like this.
15. Click the OK button in the Menu Editor dialog box. Visual Basic displays
the blank form with the newly created menu bar (see Figure 13).
17. Verify that the menu looks and works as you expect. When you click the
File menu you should see the menu commands illustrated in Figure 14.
18. When you are finished experimenting, stop the program and, optionally,
save the project.
Objective
In this exercise youll write code that runs when a menu command is selected.
Youll complete two tasks. First, youll add code to the form that ends the
program when you select the Exit menu command. Second, youll add an
About dialog box to the project and display it when you select the About menu
command.
Things to Consider
What event corresponds to a menu being clicked?
How do you end a Visual Basic program?
How can you quickly add a simple About dialog box to your
application?
Step-by-Step Instructions
1. With the form you created in the last example displayed in design mode,
select the File|Exit command from the forms menu. Visual Basic opens
the code window and creates an empty procedure named
mnuFileExit_Click.
2. Add the code to unload the form when this menu is selected. The
procedure should look like this:
4. After Visual Basic displays the form in run mode, select the File|Exit
command. Visual Basic terminates the application and returns to design
mode.
Figure 15. Add an About dialog box to the project using a template.
8. Select the About Dialog icon and click the Open button. Visual Basic
adds a new form called frmAbout to the project.
9. Add the code to open the dialog box when you select the Help|About
menu command. Begin by double-clicking Form1 in the Project window.
Visual Basic displays the form in design mode.
10. Select the Help|About menu command from the forms menu. Visual
Basic displays the forms module and creates an empty procedure called
mnuHelpAbout_Click.
11. Add the code to open the dialog box. The procedure should look like this:
13. Select the Help|About menu command. Visual Basic displays the About
dialog box in run mode (see Figure 16).
Figure 16. This dialog box was opened by selecting the Help|About menu
command.
14. Close the About dialog box and stop the program.
Objective
In this exercise you will manipulate menu properties at run time. Youll add
code to the sample application so that when you select the File|New menu
command the application will disable the New menu and change the caption of
the Save menu to Save As After choosing the Save As menu, the application
will enable the New menu and change the caption of the Save As menu back to
Save.
Step-by-Step Instructions
1. With the sample project in design mode, double-click Form1 in the
Project Window.
2. Select the File|New command from the forms menu. Visual Basic opens
the forms module and creates an empty procedure called
mnuFileNew_Click.
3. Write the code to disable the menu command and change the caption of
the Save menu to Save As. The procedure should look like this:
4. From the object list at the top of the code window, select mnuFileSave.
Visual Basic creates an empty procedure called mnuFileSave_Click.
5. Write the code to enable the New menu command and change the caption
of the Save menu back to Save. The procedure should look like this:
8. Select the File menu again. You should see that the New menu has been
disabled and that the caption of the Save menu has been changed, as
illustrated in Figure 17.
Figure 17. The File menu has been modified at run time.
9. Select the File|Save As menu item and verify that the File menu is
changed back to its original appearance.