Wyo VB Ch. 3 Lecture Notes
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.
- 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.
- 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
tha the user knows that he cannot choose that option.
- 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.
- 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 For loops.
- A For loop allows
you to repeat a section of code many times over and over again. A For loop
is called a definite loop (or determinate loop) because the
programmer knows exactly how many times the code will be repeated. Each
pass of the loop is called an iteration.
If you know exactly how many iterations are necessary for a certain task,
you should use a For loop rather than another kind of loop that you will
study later called an indefinite loop.
- Example:
For intCounter = 1 To 100
MessageBox.Show("Hello World")
Next
This loop will iterate exactly 100 times. Each time
the loop iterates the computer will display a message box due to the
statement that is inside of the body of
the loop. The variable intCounter is
called a loop variable (aka control variable). It begins
with the value of 1. Each time the loop iterates the intCounter increases
by one. So intCounter will be equal
to 2 the second time around the loop and so on. When intCounter is
equal to 100 the loop will iterate one last time. In the example above,
the initial value of intCounter is
1. Notice that the body of the For loop
is indented. Also realize that you can place as many statements in the
body of the loop as you wish. Usually, the loop variable of a
For loop is an Integer rather than a Double.
It is tradition and considered fine style to use uppercase or lowercase
j or i as the
name
for a loop
variable as in
Dim j As Integer = 0
For j = 1 To 100
MessageBox.Show("Hello World")
Next
or
For j As Integer = 1 to 100
MessageBox.Show("Hello World")
Next
but j can only be used inside the For loop if it is declared on the first line of the loop. It is "local" to the loop.
- The loop variable can change by amounts other than 1 on each iteration. The amount that the loop variable changes is called the loop's step size. The default step size for Visual Basic For loops is 1. However, you can type the keyword Step followed by any other value to change the step size.
Examples:
For j = 1 To 100 Step 12
MessageBox.Show("Hello World")
Next
For i = 100 To 1 Step -1
MessageBox.Show("Hello World")
Next
For dblValue = 0 To 1 Step 0.1
MessageBox.Show("Hello World")
Next
For j = 2 To 11 Step 2
MessageBox.Show("Hello World")
Next
Determine the number of loop iterations and the final value of the loop variable in each example above. It is important that you know how to trace a loop. When you trace a loop, you carefully follow and keep track of every variable and record the successive values of the variables in a column.
- If you use a step size of 1, do not type out the "Step 1" part of the For statement. You will lose points on programs that include this unnecessary code. It is considered to be acceptable style to use loop variables that consist of a name with one letter such as J, j, I, or i.
- If a For loop has a Step value of 1 as in
For j = Low To High
intIterations = intIterations + 1
Next
then the number of loop iterations can easily be calculated with the formula
# of loop iterations =
High - Low + 1
Be wary of the fencepost error which results from not adding 1 in this formula. The fencepost error is dangerous since it causes computer programmers to underestimate the overall number of loop iterations by one. If you built a fence with 4 fencerails then you need 5 fenceposts. That is, you need one more fencepost than fencerails.
- If you would like, you may declare the loop variable within the For loop. For example,
For j As Integer = 0 To 10
MessageBox.Show("Hello World")
Next j
is equivalent to
Dim j As Integer = 0
For j = 0 To 10
MessageBox.Show("Hello World")
Next j
except for the fact that you cannot use the loop variable j outside of the loop.
Objective #3: Use counter and accumulator statements
in For loops to keep running totals.
- A counter statement is an assignment
statement that adds a fixed amount to a variable each time it executes.
Usually a counter statement is found inside of a loop. Here are some examples
of counter statements:
intCount = intCount + 1
In the above assignment statement, the computer first adds 1
to the "old value" of the variable intCount.
Then, the computer assigns that new larger value to the same variable
intCount. This type of counter statement can be very handy when allowing
a program to run over and over again until the variable gets to a certain
size. For example, this could allow a computer game of PacMan give the
user three lives and when he has been eaten the third time, the game
knows to end.
However the value 1 doesn't have to be used in a counter statement. In these examples, other values are used....
intNum = intNum + 3
intDollars = intDollars - 5
Notice that in each case the variable has a fixed value added to it.
The
counter statement inside of this For loop counts
the number of iterations of the loop...
Dim intCount As Integer = 0
For J = 1 To 5
intCount = intCount + 1
Next
The final value of intCount is 5 and represents
the number of iterations of the For loop.
- The word increment means to add one to a variable. So the
statement intCount = intCount + 1 is
a statement that "increments" intCount.
A shortened way to write a counter statement is
intCount += 1
where the compound operator ( += )
is used as shorthand to the longer version of the statement.
- An accumulator statement is an
assignment statement found inside of a loop that keeps track of a running
total. Unlike a counter statement, this does not add the exact same increment to the
variable each time it is executed. It is useful therefore to keep a running
total of things like points in a computer game or a bank balance in a
banking program. For example, J is added to the running total intSum in each loop iteration below and J varies on each loop iteration:
Dim intSum As Integer = 0
Dim J As Integer = 0
For J = 1 To 10
intSum = intSum + J
Next
Objective #4: Animate graphics on a
form with For loops.
- You can achieve animation by simplying changing an object's Top or Left property
within a loop. For example, the following loop would
animate the picObject horizontally
across a form:
For j = 1 To 300
picObject.Left = picObject.Left + 20
Next j
- You can create a pause with the statement
Threading.Thread.Sleep(2000)
where the parameter in the parentheses is the number of milliseconds. So the following loop would make picObject animate more slowly
For j = 1 To 300
picObject.Left = picObject.Left + 20
Threading.Thread.Sleep(2000)
Next j
Objective #5: Trace double-nested For loops.
- A nested For loop is a For loop inside of another For loop. The two loops together are called double-nested For loops. Example:
Dim R As Integer = 0
Dim C As Integer = 0
For R = 1 To 3
For C = 1 To 2
lblOutput.Text = lblOutput.Text + R * C
Next
lblOutput.Text = lblOutput.Text + vbCrLF
Next
Notice how the nested For loop is indented inside of the outer For loop for good
style. Also notice how blank lines are placed around loops for good style and
increased readability.
We will trace this example in class.