velp08.htm

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

HTM
974
字号
17: Do18: UserNum = InputBox(&quot;Enter a number from 1 to 100&quot;, &quot;Ask&quot;, &quot;50&quot;)19: If (UserNum = &quot;&quot;) Then ' Check for Cancel20: ' Restore previous value21: UserNum = lblUserNum.Caption22: Exit Sub23: End If24: Loop While (UserNum &lt; 1) Or (UserNum &gt; 100)25: lblUserNum.Caption = UserNum26: End Sub27:28: Sub optBase_Click (Index As Integer)29: ' Determines what base the converted30: ' number displays in the base frame31: Select Case Index32: Case BASE10:33: ' No change34: lblBaseOut.Caption = lblUserNum.Caption35: Case BASE16:36: lblBaseOut.Caption = Hex$(lblUserNum.Caption)37: Case BASE8:38: lblBaseOut.Caption = Oct$(lblUserNum.Caption)39: End Select40: End Sub41:42: Sub optTemp_Click (Index As Integer)43: ' Determines what temperature appears44: Select Case Index45: Case CELSIUS:46: lblTempOut.Caption = GetCel(Val(lblUserNum.Caption))47: Case FAHRENHEIT:48: lblTempOut.Caption = lblUserNum.Caption49: End Select50: End Sub51:52: Sub cmdChange_Click ()53: ' Asks the user once again for the number54: ' and calls appropriate click event55: ' procedures to update two frames56: Call GetUserNum57: optbase(BASE10).Value = True58: optTemp(FAHRENHEIT).Value = True59: Call optBase_Click(BASE10)60: Call optTemp_Click(FAHRENHEIT)61: End Sub62:63: Sub cmdExit_Click ()64: End65: End Sub</FONT></PRE><BR><A NAME="E69E117"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: Define the procedure that executes right before the user sees the form.<BR><P>2: Call the subroutine procedure that asks the user for a number between 1 and 100.<BR><P>3: Activate the Base 10 option button. When the form finally appears, the Base 10 option button will be selected.<BR><P>4: Activate the Fahrenheit option button. When the form finally appears, the Fahrenheit option button will be selected.<BR><P>5: A blank line helps separate parts of a program.<BR><P>6: A remark explains subsequent code.<BR><P>7: The remark continues.<BR><P>8: Trigger a click event procedure for the option buttons with the Bases frame. This event initializes the frame.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>8: Code can trigger event procedures.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>9: Trigger a click event procedure for the option buttons with the Fahrenheit frame. This event initializes the frame.<BR><P>10: Terminate the subroutine event procedure.<BR><P>11: A blank line separates the Form_Load() procedure from the subroutine procedure that follows.<BR><P>12: Define the subroutine procedure that requests a number from the user.<BR><P>13: A remark explains subsequent code.<BR><P>14: The remark continues.<BR><P>15: The remark continues.<BR><P>16: Define a variant variable that will capture the result of the input box.<BR><P>17: Begin a loop that will ask the user for a number.<BR><P>18: Display an input box and wait for the user's response.<BR><P>19: Don't change the user's previously selected number (or the property defaults if this is the first time the user has been asked for a number) if the user clicks the Cancel command button.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>19: Always check for the user's Cancel command button selection.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>20: A remark explains subsequent code.<BR><P>21: Put the number back to its current value.<BR><P>22: Terminate the subroutine procedure early.<BR><P>23: Terminate the If that checked for the Cancel command button press.<BR><P>24: Keep asking until the user enters a valid number within the range required.<BR><P>25: The user has entered a valid number in the input box, so display the result.<BR><P>26: Terminate the subroutine procedure.<BR><P>27: A blank line separates the GetUserNum() procedure from the event procedure that follows.<BR><P>28: Define the event procedure for the Bases option button control array.<BR><P>29: A remark explains the purpose of the event procedure.<BR><P>30: The remark continues on the next line.<BR><P>31: The Index argument can be one of three values: 10, 16, or 8, as set in the Properties window for the three Index values. Test the value in the index to determine which option button to respond to.<BR><P>32: If the user clicked the Base 10 option button...<BR><P>33: A remark describes the code that follows.<BR><P>34: The label inside the Bases frame matches the user's entered number because both are base 10.<BR><P>35: If the user clicked the Base 16 option button...<BR><P>36: Display the user's entered number as an hexadecimal string.<BR><P>37: If the user clicked the Base 8 option button...<BR><P>38: Display the user's entered number as an octal string.<BR><P>39: Terminate the Select Case.<BR><P>40: Terminate the event procedure.<BR><P>41: A blank line separates the two event procedures.<BR><P>42: Define the event procedure for the Temperatures option button control array.<BR><P>43: A remark explains the purpose of the event procedure.<BR><P>44: The Index argument can be one of two values: 0 or 1, as set in the Properties window for the two Index values. Test the value in the index to determine which option button to respond to.<BR><P>45: If the user clicked the Celsius option button...<BR><P>46: Display the user's entered number as a Celsius temperature.<BR><P>47: If the user clicked the Fahrenheit option button...<BR><P>48: The label inside the Temperature's frame matches the user's entered number because both are considered to be in Fahrenheit.<BR><P>49: Terminate the Select Case statement.<BR><P>50: Terminate the end procedure.<BR><P>51: A blank line separates the event procedures.<BR><P>52: Define the event procedure that executes when the user clicks Change Number.<BR><P>53: A remark explains the purpose of the event procedure.<BR><P>54: The remark continues on the next line.<BR><P>55: The remark continues on the next line.<BR><P>56: Call the subroutine procedure that gets a number from the user. <I>No</I> arguments are required.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>56: If the subroutine procedure requires no arguments, do not use parentheses.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>57: Make the Base 10 Bases option button the default.<BR><P>58: Make the Fahrenheit 10 Bases option button the default.<BR><P>59: Trigger the event procedure that clicks the default Base 10 option button so that the Bases frame displays the user's number just entered.<BR><P>60: Trigger the event procedure that clicks the default Fahrenheit option button so that the Temperatures frame displays the user's number just entered.<BR><P>61: Terminate the event procedure.<BR><P>62: A blank line separates the event procedures.<BR><P>63: Define the event procedure for the Exit command button.<BR><P>64: End the program when this event triggers.<BR><P>65: Terminate the event procedure.<BR><BR><A NAME="E69E118"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Close the Application</B></FONT></CENTER></H4><BR><P>You can now exit the application and exit Visual Basic. The next lesson explains how to add disk file access to your applications so that you can store and retrieve long-term data in disk files.<BR><P ALIGN=LEFT><A HREF="vel16.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel17.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>

⌨️ 快捷键说明

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