velp10.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,218 行 · 第 1/2 页

HTM
1,218
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp10.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel21.htm"><LINK REL="Previous" HREF="vel20.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp10.htm</H2><P ALIGN=LEFT><A HREF="vel20.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel21.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E154" >Stop &amp; Type</A><UL><LI><A HREF="#E69E138" >The Program's Description</A><LI><A HREF="#E69E139" >Needed: Program Randomness</A></UL><LI><A HREF="#E68E155" >The Help Menu</A><UL><LI><A HREF="#E69E140" >Descriptions</A><LI><A HREF="#E69E141" >Studying the Rest of the Code</A><LI><A HREF="#E69E142" >Descriptions</A><LI><A HREF="#E69E143" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E30"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 10</B></FONT></CENTER></H1><BR><A NAME="E67E33"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Making Programs &quot;Real World&quot;</B></FONT></CENTER></H2><BR><BR><A NAME="E68E154"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop &amp; Type</B></FONT></CENTER></H3><BR><P>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.<BR><P>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.<BR><P>In this lesson, you saw the following:<BR><UL><LI>How to create menus for your Visual Basic applications<BR><BR><LI>Why the Menu Design window is so much more appropriate for specifying menu property values than the Property window<BR><BR><LI>How to right justify menu items if you want to change the menu bar a bit<BR><BR><LI>What the timer control does to generate events<BR><BR><LI>When to respond to a timer's event and when to ignore the event<BR><BR></UL><BR><A NAME="E69E138"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>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.<BR><P><B> <A HREF="P10vel01.gif">Figure P10.1. Project 10's application begins with a simple form.</A></B><BR><P>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.<BR><BR><A NAME="E69E139"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Needed: Program Randomness</B></FONT></CENTER></H4><BR><P>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 <I>not</I> do the same thing every time you execute the same code.<BR><P>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:<BR><BR><PRE><FONT COLOR="#000080">Int((LowValue + HighValue + 1) * Rnd + LowValue</FONT></PRE><P>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:<BR><BR><PRE><FONT COLOR="#000080">Letter = Chr$(Int(26) * Rnd + 65) ' A through Z</FONT></PRE><P>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:<BR><BR><PRE><FONT COLOR="#000080">Randomize Timer</FONT></PRE><P>The Randomize command requires a single value. That value <I>seeds</I> 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.<BR><BR><A NAME="E68E155"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Help Menu</B></FONT></CENTER></H3><BR><P>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 <A HREF="vel19.htm">Unit 19</A>, the backspace character right justifies menu bar commands.<BR><P>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.<BR><P><B> <A HREF="P10vel02.gif">Figure P10.2. The Help menu command produces a program overview for the </B><B>user.</A></B><BR><P><FONT COLOR="#000080"><B>Listing P10.1. Respond to the Help command and turn off help when requested.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub mnuHelp_Click ()2: ' Show the help message and the help command button3: lblHelp.Visible = True4: cmdHelp.Visible = True5: End Sub6:7: Sub cmdHelp_Click ()8: ' Hide the help message and the help command button9: lblHelp.Visible = False10: cmdHelp.Visible = False12: End Sub</FONT></PRE><BR><A NAME="E69E140"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The code for the event procedure that responds to the Help menu bar command must be a click procedure.<BR><P>2: A remark that explains the procedure.<BR><P>3: Turn on the large label that appears in the center of the screen.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>3: The help is a label that normally remains invisible.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>4: Turn on the command button that the user can use to hide the help information.<BR><P>5: Terminate the procedure.<BR><P>6: A blank line separates the procedures.<BR><P>7: The code for the event procedure that responds to the command button that turns off Help's information.<BR><P>8: A remark that explains the procedure.<BR><P>9: Turn off the large label that appears in the center of the screen.<BR><P>10: Turn off the command button that the user can use to hide the help information.<BR><P>11: Terminate the procedure.<BR><BR><A NAME="E69E141"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Studying the Rest of the Code</B></FONT></CENTER></H4><BR><P>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.<BR><P><B> <A HREF="P10vel03.gif">Figure P10.3. The user must guess a letter from A to Z.</A></B><BR><P><FONT COLOR="#000080"><B>Listing P10.2. The code for the guessing-game project listing.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: ' Begin with a number-guessing game3: ' Turn off the checkmark from the4: ' Options Use Alphabetic Characters menu5: mnuOptionUseAlpha.Checked = False6: ' Right-justify the Help menu command7: mnuHelp.Caption = Chr$(8) + mnuHelp.Caption8: ' Spin the internal randomizing wheel9: Randomize Timer10: End Sub11:12: Sub mnuFileExit_Click ()13: End14: End Sub15:16: Sub mnuFileNewGame_Click ()17: ' Call appropriate subroutine procedure depending18: ' on value of Option Use Alphabetic Characters19:20: ' Turn off opening label if it's still visible21: If (lblOpening.Visible = True) Then22: lblOpening.Visible = False23: End If24:25: If (mnuOptionUseAlpha.Checked) Then26: Call GuessLetter27: Else28: Call GuessNumber29: End If30: ' Turn back on opening label31: lblOpening.Visible = True32: End Sub33:34: Sub mnuOptionUseAlpha_Click ()35: ' Reverse the state of the checkmark36: mnuOptionUseAlpha.Checked = Not (mnuOptionUseAlpha.Checked)37: End Sub38:39: Sub GuessNumber ()40: ' User guesses a number41: Dim Guess As Variant42: Dim Number, GuessNum As Integer43:44: ' Update the instruction label45: lblInstruct.Caption = &quot;Guess a number from 1 to 100&quot;46: lblInstruct.Visible = True47: ' Find value from 1 to 10048: Number = Int(100 * Rnd + 1)49:50: Do51: Guess = InputBox(&quot;What's your guess?&quot;, &quot;Guess&quot;)52: If (Guess = &quot;&quot;) Then ' Check for Cancel53: ' Turn off instruction label54: lblInstruct.Visible = False55: Exit Sub ' User pressed Cancel56: End If57: GuessNum = Guess ' Convert to number58: If (GuessNum &gt; Number) Then59: MsgBox &quot;Your number is too high...&quot;, , &quot;Wrong&quot;60: Else61: If (GuessNum &lt; Number) Then62: MsgBox &quot;Your number is too low...&quot;, , &quot;Wrong&quot;63: End If64: End If65: Loop Until (GuessNum = Number)66: Beep67: MsgBox &quot;You guessed &quot; &amp; Guess &amp; &quot;! Correct!!!&quot;, , &quot;Lucky&quot;68: ' Turn off instruction label69: lblInstruct.Visible = False70: End Sub71:72: Sub GuessLetter ()73: ' User guesses a letter74: Dim Letter, GuessLet As String75:76: ' Update the instruction label77: lblInstruct.Caption = &quot;Guess a letter from A to Z&quot;78: lblInstruct.Visible = True79: ' Find value from ASCII 65 - 90 ('A' to 'Z')80: Letter = Chr$(Int(26) * Rnd + 65) ' A through Z81: Do82: GuessLet = InputBox(&quot;What's your guess?&quot;, &quot;Guess&quot;)83: If (GuessLet = &quot;&quot;) Then ' 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?