This lesson taught you how to design and add menus to your Visual Basic applications. The Menu Design window enables you to specify the property values for menu bar commands and pull-down menu items. By using the
Menu Design window, you can add shortcut
access keystrokes and menu separator bars, and specify special menu features such as checkmarked menu items. Generating program code that responds to menu events is no more difficult than writing event procedures
that respond to command button clicks.
In addition to creating menus, you also learned how to work with the timer control. As you saw in the previous unit, the timer control is a control that triggers its own events. Depending on the value of the
timer control's Interval property, the timer
control generates an event every few milliseconds. The code that you add to the timer's event procedure determines how to handle the event. You can set alarms and perform certain screen and control update
actions every time that the timer generates an
event, if you want.
In this lesson, you saw the following:
Figure P10.1 shows the
PROJEC10.MAK opening Form window. This project presents a simple number- and letter-guessing that contains a menu bar with three items. The program contains three event procedures that are tied to the menu's commands.
Figure P10.1. Project 10's application begins with a simple form.
The Menu Design window is nothing more than an advanced Property window for the menu. The menu events to which your code will respond are click events.
Therefore, there is nothing really new with the code used in this program. The program generates a
random number or letter when the user selects File New Game. If the Option Use Alphabetic Letters menu option is checked, the program will generate a random
letter from A through Z. If the Option Use Alphabetic Letters menu option isn't checked, the
program will generate a random number from 1 to 100. A small label at the bottom of the guess screen reminds the user to guess a number or letter when the
guessing begins.
Because there is nothing new required for code that responds to menu events,
this lesson's project introduces a new command and function to generate the random value and make the project more interesting. The random number generator is the only part of
Visual Basic's powerful language that does not do the same thing every
time you execute the same code.
The Rnd() function, when used without an argument, generates a random number between 0 and 1. If the user wants to guess a number, however, the program needs a random number from 1 to 100, and if the user wants to
guess a letter, the program needs a
random number from 65 to 90 to represent Chr$() arguments for the letters A through Z. The program uses the following formula to generate a random number from a low value to a high value:
Int((LowValue + HighValue + 1) * Rnd + LowValue
Therefore, here is the assignment statement that generates a random number from 65 to 90, and stores the character value of that generated ASCII number in a variable named
Letter:
Letter = Chr$(Int(26) * Rnd + 65) ' A through Z
There is only one problem with the Rnd function. The function generates the same set of random values every time you run a program that uses
Rnd, unless you first execute the following statement:
Randomize Timer
The Randomize command requires a single value. That value seeds the random number generator by providing a value for
the first generated random number. If you were to use the same seed value, Rnd wouldn't change from program run to program run. A
guessing game that always produced the same set of values to guess wouldn't remain interesting. By using Timer to seed the
internal random number generator, you can ensure that Randomize rarely works with the same value twice, because the randomizing seed
value is the internal clock setting at the time that the Randomize statement executes.
You'll notice that the Help menu is right justified. The Form_Load() event procedure concatenates a Chr$(8) backspace character to the menu bar's
Help command. As you learned in Unit 19, the backspace character right justifies
menu bar commands.
Figure P10.1 shows the help message and command button that appear when the user selects the Help command. The Help
command's event procedure, shown in Listing P10.1, sets the Visible property of the command button and help label to True so that the
user can see the help message. When the user clicks the command button, the cmdHelp_Click() event procedure (also shown
in Listing P10.1) executes to hide the help label and command button.
Figure P10.2. The Help menu command produces a program overview for the user.
Listing P10.1.
Respond to the Help command and turn off help when requested.
1: Sub mnuHelp_Click () 2: ' Show the help message and the help command button 3: lblHelp.Visible = True 4: cmdHelp.Visible = True 5: End Sub 6: 7: Sub cmdHelp_Click () 8: ' Hide the help message and the help command button 9: lblHelp.Visible = False 10: cmdHelp.Visible = False 12: End Sub
1: The code for the event procedure that responds to the Help menu bar command must be a click procedure.
2: A remark that explains the procedure.
3: Turn on the large
label that appears in the center of the screen.
3: The help is a label that normally remains invisible.
4: Turn on the command
button that the user can use to hide the help information.
5: Terminate the procedure.
6: A blank line separates the procedures.
7: The code for the event procedure that responds to the command button that turns off Help's
information.
8: A remark that explains the procedure.
9: Turn off the large label that appears in the center of the screen.
10: Turn off the command button that the user can use to hide the help information.
Listing P10.2 contains the rest of the program, including the
Form_Load() event procedure that right justifies the Help command and seeds the random number generator so that subsequent calls to Rnd will produce a different set of values between program
runs. Figure P10.3 shows the running program during the user's
guessing of a letter. Notice that a label appears at the bottom of the screen during the progress of the game that tells the user whether letters or numbers are to be guessed.
Figure P10.3. The user must guess a letter
from A to Z.
Listing P10.2. The code for the guessing-game project listing.
1: Sub Form_Load ()
2: ' Begin with a number-guessing game
3: ' Turn off the checkmark
from the
4: ' Options Use Alphabetic Characters menu
5: mnuOptionUseAlpha.Checked = False
6: ' Right-justify the Help menu command
7: mnuHelp.Caption = Chr$(8) + mnuHelp.Caption
8: ' Spin the internal randomizing wheel
9: Randomize Timer
10: End Sub
11:
12: Sub mnuFileExit_Click ()
13: End
14: End Sub
15:
16: Sub mnuFileNewGame_Click ()
17: ' Call appropriate subroutine procedure depending
18: ' on value of Option Use Alphabetic Characters
19:
20: ' Turn off opening label if it's still
visible
21: If (lblOpening.Visible = True) Then
22: lblOpening.Visible = False
23: End If
24:
25: If (mnuOptionUseAlpha.Checked) Then
26: Call GuessLetter
27: Else
28: Call GuessNumber
29: End If
30: ' Turn back on opening label
31:
lblOpening.Visible = True
32: End Sub
33:
34: Sub mnuOptionUseAlpha_Click ()
35: ' Reverse the state of the checkmark
36: mnuOptionUseAlpha.Checked = Not (mnuOptionUseAlpha.Checked)
37: End Sub
38:
39: Sub GuessNumber ()
40: ' User guesses a
number
41: Dim Guess As Variant
42: Dim Number, GuessNum As Integer
43:
44: ' Update the instruction label
45: lblInstruct.Caption = "Guess a number from 1 to 100"
46: lblInstruct.Visible = True
47: ' Find value from 1 to 100
48: Number =
Int(100 * Rnd + 1)
49:
50: Do
51: Guess = InputBox("What's your guess?", "Guess")
52: If (Guess = "") Then ' Check for Cancel
53: ' Turn off instruction label
54: lblInstruct.Visible = False
55: Exit Sub ' User pressed
Cancel
56: End If
57: GuessNum = Guess ' Convert to number
58: If (GuessNum > Number) Then
59: MsgBox "Your number is too high...", , "Wrong"
60: Else
61: If (GuessNum < Number) Then
62: MsgBox "Your number is too
low...", , "Wrong"
63: End If
64: End If
65: Loop Until (GuessNum = Number)
66: Beep
67: MsgBox "You guessed " & Guess & "! Correct!!!", , "Lucky"
68: ' Turn off instruction label
69:
lblInstruct.Visible = False
70: End Sub
71:
72: Sub GuessLetter ()
73: ' User guesses a letter
74: Dim Letter, GuessLet As String
75:
76: ' Update the instruction label
77: lblInstruct.Caption = "Guess a letter from A to Z"
78:
lblInstruct.Visible = True
79: ' Find value from ASCII 65 - 90 ('A' to 'Z')
80: Letter = Chr$(Int(26) * Rnd + 65) ' A through Z
81: Do
82: GuessLet = InputBox("What's your guess?", "Guess")
83: If (GuessLet = "") Then '
Check for Cancel
84: ' Turn off instruction label
85: lblInstruct.Visible = False
86: Exit Sub ' User pressed Cancel
87: End If
88: ' Convert user's letter to uppercase
89: GuessLet = UCase(GuessLet)
90: If (GuessLet > Letter) Then
91: MsgBox
"Your letter is too high...", , "Wrong"
92: Else
93: If (GuessLet < Letter) Then
94: MsgBox "Your letter is too low...", , "Wrong"
95: End If
96: End If
97: Loop Until (GuessLet = Letter)
98:
99: ' Here is
user guessed number
100: Beep
101: MsgBox "You guessed " & GuessLet & "! Correct!!!", , "Lucky"
102:
103: ' Turn off instruction label
104: lblInstruct.Visible = False
105: End Sub
1: The code for the opening event procedure that executes right before the user sees the form.
2: A remark explains
the procedure.
3: The remark continues.
4: The remark continues.
5: Set the guess to letters.
6: A remark explains the code.
7: Right justify the Help menu option.
8: A remark explains the code.
9: Use the internal clock to seed the random number generator.
9: Be sure to seed the random number generator so that truly random values appear.
10: Terminate the procedure.
11: A blank line separates the procedures.
12: The code for the File Exit menu command.
13: Terminate the program's execution.
14: Terminate the procedure.
15: A blank line separates the procedure.
16: The code for the File New Game menu command.
17: A remark explains the procedure.
18: The remark continues.
19: A blank line helps separate parts of the program.
20: A remark that explains the procedure.
21: Turn off the display of the Press File New Game label if it's on.
22: Hide the label.
23: Terminate the If.
24: A blank line helps separate parts of the program.
25: See whether the user wants to guess a letter.
26: Execute the subroutine procedure that enables the user to guess a letter.
27: If the user wants to guess a number...
28: Execute the subroutine procedure that
enables the user to guess a number.
29: Terminate the If.
30: A remark explains the code.
31: Now that the user has finished the game, turn the Select File New Game label.
32: Terminate the procedure.
33: A
blank line helps separate parts of the program.
34: The code for the Option use Alphabetic Letters menu command.
35: A remark helps explain the procedure.
35: Check or uncheck the menu command when clicked.
36: Turn the menu command's check mark either on or off.
37: Terminate the procedure.
38: A blank line helps separate the procedures.
39: The subroutine procedure called when the user wants to guess a number.
40: A remark explains the procedure.
41: Define a variable to hold the InputBox() function's return value.
42: Define variables to hold the
user's guess and the random number generated for the guessing game.
43: A blank line helps separate parts of the procedure.
44: A remark explains the code.
45: Change the label that appears at the bottom of the screen during
the user's guessing.
46: Displays the guessing label.
47: A remark explains the code.
48: Generate a random number from 1 to 100.
48: Unless you convert the value fall within a different range, Visual Basic generates a number from 0 to 1.
49: A blank line helps separate parts of the code.
50: Begin the guessing loop.
51: Get
the user's guess.
52: Check to see whether the user pressed the Cancel command button at the display of the input box.
53: A remark explains the code.
54: Turn off the guessing label.
55: Terminate the guessing
subroutine early.
56: Terminate the If.
57: Convert the user's Variant answer to an Integer.
58: Check whether the user's guess was too high.
59: If the user's guess was too high, tells the user.
60: The
user's guess was not too high.
61: Check whether the user's guess was too low.
62: If the user's guess was too low, tells the user.
63: Terminate the If.
64: Terminate the If.
65: Keep asking the user for a
guess until the user guesses the correct answer.
66: Audibly tells the user that the guess was correct.
67: Show the user that the guess was correct.
68: A remark helps explain the code.
69: Hide the guessing
instructional label.
70: Terminate the procedure.
71: A blank line helps separate procedures.
72: The subroutine procedure called when the user wants to guess a letter.
73: A remark explains the procedure.
74: Define a variable to hold the user's guess and the random letter.
75: A blank line helps separate parts of the procedure.
76: A remark explains the code.
77: Change the label that appears at the bottom of the screen
during the user's guessing.
78: Display the guessing label.
79: A remark explains the code.
80: Generate a random letter from A to Z.
81: Begin the guessing loop.
82: Get the user's guess.
83:
Check to see whether the user pressed the Cancel command button at the display of the input box.
84: A remark explains the code.
85: Turn off the guessing label.
86: Terminate the guessing subroutine early.
87:
Terminate the If.
88: A remark explains the code.
89: Convert the user's guess to uppercase if the letter is not already uppercase.
89: The UCase$() function keeps the user's guessing range within the uppercase letters.
90: Check whether the user's guess was too high.
91: If the user's guess was too high, tells the user.
92: The user's
guess was not too high.
93: Check whether the user's guess was too low.
94: If the user's guess was too low, tells the user.
95: Terminate the If.
96: Terminate the If.
97: Keep asking the user for a guess
until the user guesses the correct answer.
98: A blank line helps separate parts of the code.
99: A remark helps explain the code.
100: Audibly tells the user that the guess was correct.
101: Show the user that the
guess was correct.
102: A blank line helps separate parts of the code.
103: A remark helps explain the code.
104: Hide the guessing instructional label.
You can now exit the application and exit Visual Basic. The next lesson teaches you how to send text to the printer and really have fun by drawing graphics on the user's form.