vel09.htm

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

HTM
2,065
字号
<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDRETRY</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the Retry button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>5</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDIGNORE</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the Ignore button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>6</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDYES</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the Yes button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>7</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDNO</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the No button</FONT></TABLE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 9.3 shows how you might handle the previous MsgBox() function that displayed the three-button message box in Figure 9.6. After the user clicks a button, the program can use If statements to determine which button the user pressed.<BR><P><FONT COLOR="#000080"><B>Listing 9.2. Displaying different message boxes.</B></FONT><BR><PRE><FONT COLOR="#000080">1: BPress = MsgBox(&quot;Are you ready for the report?&quot;, MB_ICONQUESTION + MB_YESNOCANCEL, &quot;Report Request&quot;)2: ' Check the button pressed3: Select BPress4: Case IDCANCEL: lblPress.Text = &quot;You pressed Cancel&quot;5: Case IDYES: lblPress.Text = &quot;You pressed Yes&quot;6: Case IDNO: lblPress.Text = &quot;You pressed No&quot;7: End Select</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 displays the message box and saves the button press in the variable named BPress. Line 3 begins a Select Case that assigns one of three strings to labels on the form that match the user's button press. This example is very simple; normally, you would perform one of several different kinds of routines, depending on which of the three buttons the user pressed.<BR><P>There is no need to code a Case Else statement because the three-button message box can return only one of the three values tested in Listing 9.2.<BR><BR><A NAME="E68E76"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>InputBox()</B><B> Functions</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>You'll find that the InputBox() function is easy because it acts a lot like the MsgBox() function. There are two InputBox() functions. The difference between them lies in the type of data that each returns. The InputBox() function receives answers that are more complete than the MsgBox() function can get. Whereas MsgBox() returns one of seven values that indicate the user's command button press, the InputBox() function returns either a string or a variant data value that holds the answer typed by the user.<BR><P>There are two InputBox() functions. Here are the formats of the InputBox() functions:<BR><BR><PRE><FONT COLOR="#000080">aVariantVariable = InputBox( prompt [, [title] [, default][, xpos, ypos]]])</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">aStringVariable = InputBox$( prompt [, [title] [, default][, xpos, ypos]]])</FONT></PRE><P>The difference between the InputBox() functions lies in the return value. The InputBox() function returns a variant data type and the InputBox$() function (notice the dollar sign) returns a string data type. Generally, the return type is not extremely important. You'll almost always receive the InputBox() answer in a string variable, and if you use the InputBox() function, Visual Basic converts variant data types to strings when needed.<BR><P>The <I>prompt</I> works a lot like the <I>msg</I> value in a MsgBox() function. The user sees the <I>prompt</I> inside the input box displayed on the screen. The <I>title</I> is the title inside the input box's title bar. <I>default</I> is a default string value that Visual Basic displays for a default answer, and the user can accept the default answer or change the default answer.<BR><P>The <I>xpos</I> and <I>ypos</I> positions indicate the exact location where you want the input box to appear on the form. The <I>xpos</I> value holds the number of twips from the left edge of the form window to the left edge of the input box. The <I>ypos</I> value holds the number of twips from the top edge of the form window to the top edge of the input box. If you omit the <I>xpos</I> and <I>ypos</I> values, Visual Basic centers the message box on the form.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Input boxes always contain an OK command button and a Cancel command button. If the user presses OK (or presses Enter, which selects OK by default), the answer in the input box is sent to the variable being assigned the returned value. If the user presses Cancel, a null string, &quot;&quot;, returns from the InputBox() function.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>The code in Listing 9.3 displays an input box that asks the user for a company name. The user either enters a response to the prompt or presses the Cancel command button to indicate that no answer is coming.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The InputBox() and InputBox$() functions get answers from users. You can offer a default answer that the user can accept or change. The input box functions return the answer into a string or variant variable to which you assign the function.<BR><P><FONT COLOR="#000080"><B>Listing 9.3. The input box asks the user questions.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim CompName As String2: CompName = InputBox$(&quot;What is the name of the company?&quot;, &quot;Company Request&quot;, &quot;XYZ, Inc.&quot;)3: If (CompName = &quot;&quot;) Then4: ' The user pressed Cancel5: Beep6: MsgBox &quot;Thanks anyway&quot;7: Else8: ' The user entered a company name9: MsgBox &quot;You entered &quot; &amp; CompName10: End If</FONT></PRE><P>[ic:Output]Figure 9.7 contains the message box displayed from Listing 9.3.<BR><P><B> <A HREF="09vel07.gif">Figure 9.7. Input boxes ask the users questions and return the answers.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Listing 9.3 might be part of an event procedure that executes when the program is ready for a company name. Line 1 defines a string variable that holds the user's response. Line 2 contains the InputBox$() function that asks for the company name and displays a default answer (automatically highlighted as shown at the bottom of Figure 9.7).<BR><P>As soon as the user answers the input box request, Line 3 begins an if that checks for one of two results: either the user pressed the Cancel button in response to the input box, or answered the input box by pressing OK. If the user pressed Cancel, the input box function returns a null string, and lines 5 and 6 beep and display a message box thanking the user for trying. Lines 8 and 9 restate the company name entered by the user.<BR><BR><A NAME="E68E77"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E64"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is a remark?<BR><BR><LI>Why are remarks important?<BR><BR><LI>True or false: Remarks appear on the user's screen.<BR><BR><LI>True or false: Remarks appear in the Code window.<BR><BR><LI>How many kinds of remarks does Visual Basic support?<BR><BR><LI>Name three uses for remarks.<BR><BR><LI>Who are remarks for?<BR><BR><LI>True or false: There is no reason to worry about remarks if you're the only programmer who will work on your program now or later.<BR><BR><LI>True or false: Message boxes are controls.<BR><BR><LI>How can you control the number of command buttons that appear on message boxes?<BR><BR><LI>How can you control the text that appears on message boxes?<BR><BR><LI>What does <I>modal</I> mean?<BR><BR><LI>True or false: Visual Basic includes a message box statement and a message box function.<BR><BR><LI>True or false: Visual Basic includes an input box statement and an input box function.<BR><BR><LI>Why would you use an input box rather than a message box?<BR><BR><LI>What data types can the input box functions return?<BR><BR><LI>How can you check to make sure that the user pressed Cancel in response to an input box?<BR><BR><LI>Suppose that you used King George, III as a default value in an input box function. What does the user have to do to use that default value?<BR><BR><LI>How many kinds of icons can you display in message boxes?<BR><BR><LI>How many kinds of icons can you display in input boxes?<BR><BR><LI>How can you control the command button focus with message boxes?<BR><BR><LI>How many different command buttons can you display in message boxes?<BR><BR><LI>How many return values are there for the MsgBox() functions?<BR><BR></OL><BR><A NAME="E69E65"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>If the code that you write senses that a particular disk file is corrupted, you want to inform the user of the serious error and you want to make sure that the user can't switch to another Windows program before responding to your error message box. What <I>type</I> argument would you use in the MsgBox statement?<BR><BR></OL><P><B>Find the Bug</B><OL><LI>Bob the programmer knows how important program documentation is. Despite his best efforts, Bob gets errors when he tries to add the following remark to the end of a line of code:<BR>do until (endOfFile) Rem Continue until the end<BR>Help Bob with his problem.<BR><BR></OL><BR><A NAME="E69E66"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write a command button Click routine that asks the user for his or her age. If the user presses Cancel, beep and display another message box saying, Thanks anyway. If the user enters an age, use Val() to convert and store the age in a numeric variable and then display the number of years until the user's retirement by subtracting the age from 65. Hint: The Str$() function performs the opposite of Val() and you can append the converted number to another string to display the message. (Lesson 7 covers Str$() in full detail.)<BR><P ALIGN=LEFT><A HREF="velp04.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="vel10.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 + -
显示快捷键?