CIS 230, Visual Basic
Ch. 5 Notes
Objective #1: Create menus and submenus for program control.
- Most modern Windows programs use common Windows menus to give the user
direct access to key functions and commands. Visual Basic makes it easy to create such
menus in your programs via the Menu Editor from the Tools menu. While it
is true that anything done from a menu option could also be done from a command button, it
is sometimes better to give users drop-down menu commands than to clutter a form with a
lot of command buttons.
- Menu commands are controls like command buttons, text boxes, etc. Each menu command has
its own Name and Caption properties. Menu commands can respond to Click events as well.
The prefix for a menu control is "mnu".
- It is easy to use the special Menu Editor window to create menus.
- You should follow Windows standards regarding the availability and placement of
particular menu commands. For example, your first menu Caption should be File, with
'F' as the access key. Therefore, the Caption for this menu command would be &File.
- Furthermore, you should follow the convention of including the top entry on a menu
within the Name properties of other menu commands on the same menu. For the Print command
which should be placed somewhere on the File menu you should use the name mnuFilePrint.
- When a user clicks one of the menu commands like the Exit command (which should be on
the File menu by the way) the mnuFileExit_Click event would trigger.
- A submenu is indicated by a triangle to the right of a menu command.
The triangle indicates that the user can select from several other menu commands by moving
his/her mouse over the arrow. Often these submenu commands appear to the right of the
original pull-down menu.
- Within the Menu Editor it is possible to set certain menu commands as submenu commands
by clicking on the Right arrow button so that dots (....) appear and the submenu command
is indented.
- It is also possible to add separator bars within menus to indicate
certain groupings of commands according to their purpose. A separator bar will be created
if you type a hyphen (-) in the Caption of a menu command.
- To add code to a menu command, you simply select the menu command in design time to see
its code window.
- The Checked, Enabled, and Visible properties of menu commands can be used to provide a
standard Windows look and feel to your menu. They can also make your program more user
friendly.
- Within a menu command's code, you can cause the menu command to appear with a check mark
with a statement like mnuViewToolbar.Checked
= True
- You can also use keyboard shortcuts which are invoked by having the
user press Ctrl + [another key] to invoke menu commands which are not on top of menu list
(as opposed to Alt + [another key] for access keys). The Menu Editor
allows keyboard shortcuts (like Ctrl + P for Print) to be set up with menu commands.
Objective #2: Display and use the Windows common dialog boxes.
- There are a number of predefined, standard Windows dialog boxes that
can be used in your VB project. You first have to add the common dialog control (prefix,
dlg) to your form. This control will be invisible during run time but you must place it on
the form in design time in order to make use of common dialog boxes. You only need one
common dialog control on your form to be able to use it numerous times.
- To have the standard Color dialog box appear on the screen, you use the
statement
dlgCommon.ShowColor (where dlgCommon
is the name that you gave to the common dialog control on your form and ShowColor is a
method associated with the common dialog box). Then you could use the color selected by
the user in your program since the user's selection will be stored in the Color property
of the common dialog box. So the statement
frmForm1.Backcolor
= dlgCommon.Color would change
the background color of your form (named Form1) to the color selected by the user.
- In some cases you must set the special Flags property of a dialog box
to a certain value in order to be able to access the range of possible values for another
property. For example, VB needs to determine your system's available fonts before it
allows the Font property to be changed.
- You can provide a more user friendly interface by determining current values of certain
object properties and then preselecting those values in dialog boxes that appear to the
user. See the code on pp. 180 & 181.
Objective #3: Write reusable code in sub procedures and function procedures and
call the procedures from other locations.
- Programmers should strive to reuse code whenever practical.
Therefore placing code that can be used in multiple situations into general
procedures (as opposed to event procedures) is wise. For example,
you may provide a menu option that prints a form as well as a command button
that prints a form. Rather than duplicating the necessary code that actually
prints the form and placing that code into two areas of your project, you
can write one general procedure. That general procedure can then be called
from several event procedures as often as necessary.
- To create a general procedure, you must display the Code window for the
form. Then select Add Procedure from the Tools menu. Choose Sub as the Type
if you wish to create a sub procedure and choose Function
if you want to create a function procedure. Either way, you
should usually select Private for the Scope of the procedure. Selecting Public
as the scope allows other modules (forms and code modules to use the procedure).
- It is often necessary for an event procedure, which needs to make use of
a general procedure, to pass one or more values to the general
procedure. This could be done by declaring the variable(s) to be passed with
module-level or even global scope. However, one should always avoid using
any more global (or module-level) scope variables than necessary. That is,
you should always use as narrow scopes as possible with all variables. Therefore
to get around this you can pass values to the general procedure as arguments.
(Arguments are sometimes called parameters even though there
is a technical difference. I will use the term "arguments" throughout
this discussion for simplicity.)
- To pass an argument, you name it along with its data type within the parentheses
that follow the name of the general procedure in its header.
(The procedure header is the first line of the procedure's definition.) The
argument being sent to the general procedure does not (and should not) have
the same name as the corresponding argument listed in that general procedure's
header. But the data types must match. If you pass more than one argument,
you must be sure to list them in order that they need to be received and match
their data types as well.
- You can pass arguments to a procedure by value
or by reference. Passing an argument by value causes a copy
of the argument to be used in the called procedure. The original
argument within the calling procedure is not changed or affected.
Passing an argument by reference allows the procedure to permanently change
the value of the corresponding argument in the calling procedure (even if
it has a different name). By default, VB will pass an argument by reference.
If you wish to pass an argument by value, you must use the keyword ByVal
before the argument's name in the procedure header. (ByRef
can be used to pass by reference even though it is the default.)
- VB allows you to create two kinds of general procedures, function procedures
(or simply called functions) and sub procedures. There are several key differences
between the two. A function procedure works almost identically as a sub procedure
however a function should be used when the reusable code is only meant to
compute and return one single value. That returned value
can be of any data type but the function cannot perform actions like a procedure
can. Usually functions are single-minded in purpose. You must assign a value
to the name of the function within its definition. That value is then returned
and used within a statement in the calling procedure. Therefore a statement
that calls a function procedure such as
intAdd = intAddTwoNums(intNum1, intNum2)
always looks different from a statement that calls a sub procedure
AddTwoNumsAndPrint intNum1, intNum2
However, this sub procedure call statement can also be written with the keyword
Call and parentheses surrounding the arguments as in
Call AddTwoNumsAndPrint( intNum1, intNum2)
Anyway, the purpose of the procedure AddTwoNumsAndPrint is to compute a sum
and print that sum while the purpose of the function intAddTwoNums is only
to compute the actual sum. Sub procedures perform tasks while function procedures
are usually only meant to compute and return a specific value. Note that you
should include the proper prefix that indicates the return type of a function
in a functions identifier (name).
Objective #4: Create an executable file that can be run from the Windows
environment.
- To create an executable file select Make... from the File menu. This
will not affect your actual project. The new executable file will have the extension
.exe and can be run from the Windows desktop.
CIS 230 Home Page | Mr. Minich's Education Home Page | Minich.com Web Design