vel09.htm

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

HTM
2,065
字号
<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Add remarks throughout a Visual Basic program, using either the Rem statement or the apostrophe, to tell other programmers as well as yourself what the program is doing. Add the remarks as you write the program so that the remarks will be up to date and always reflect your intent.<BR><PRE><FONT COLOR="#000080">1: Rem The following Select Case to End Select code2: Rem assigns a student's grade and school name3: Rem to the label on the form. The code checks4: Rem to make sure that the student is not too5: Rem young to be going to school.6: Select Case Age7: ' Check for too young...8: Case Is &lt;5: lblTitle.Text = &quot;Too young&quot;9: ' Five-year olds are next assigned10: Case 5: lblTitle.Text = &quot;Kindergarten&quot;11: ' Six to eleven...12: Case 6 To 11: lblTitle.Text = &quot;Elementary&quot;13: lblSchool.Text = &quot;Lincoln&quot;14: ' Twelve to fifteen...15: Case 12 To 15: lblTitle.Text = &quot;Intermediate&quot;16: lblSchool.Text = &quot;Washington&quot;17: ' Sixteen to eighteen18: Case 16 To 18: lblTitle.Text = &quot;High School&quot;19: lblSchool.Text = &quot;Betsy Ross&quot;20: ' Everyone else must go to college21: Case Else: lblTitle.Text = &quot;College&quot;22: lblSchool.Text = &quot;University&quot;23: End Select</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Indention was used in Listing 9.1's remarks to put the remarks where they would be most effective. The remarks on lines 7, 9, 11, 14, 17, and 20 don't appear at the end of their respective code lines because the remarks would hang too far out to the right and would not fit in the Code window. Nevertheless, the remarks do clarify each Case in the sequence. Notice that the first few lines, 1 through 5, give an overall descriptive account of the code that follows.<BR><BR><A NAME="E68E73"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Introduction to Message and Input Boxes</B></FONT></CENTER></H3><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Message boxes</I> perform specialized program output.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>There will be many times in programs when you'll need to ask the user questions or display error messages and advice to the user. Often, the controls on the form won't work well for such dialogs. For example, in the Lesson 4's project, the user had to enter 0 through 4 when asked for a discount percentage code. If the user entered an incorrect code, the program beeped and erased the user's bad entry, but the program didn't tell the user why the entry was a mistake. Users don't always know what's expected of them. The user would be helped much more by seeing an error message, such as the one shown in Figure 9.1, before the program cleared the user's incorrect entry.<BR><P><B> <A HREF="09vel01.gif">Figure 9.1. A message box can provide the user with helpful information.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Message boxes aren't controls. Unlike controls that stay on the form throughout a program's entire execution cycle, a message box pops up on top of the form and disappears when the user responds to the message box, usually by clicking the message box's OK command button.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>There are two ways to produce message boxes. You can use the MsgBox statement or the MsgBox() function. You've seen one built-in function before, Val(), which converts a string of digits to a number. Visual Basic includes several built-in functions; you'll learn many of them in Lesson 7, &quot;Functions and Dates.&quot; Until Lesson 7, however, you need to learn a few of the functions such as Val() and MsgBox(), because they help you work with the commands that come before Lesson 7. The choice of using the MsgBox statement or the MsgBox() function depends on the response that you need the user to make to the display of the message box.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Input boxes</I> perform specialized program input.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The text box controls that you've seen are great for getting values from the user. Other controls that you'll learn as you progress through this book also accept the user's input from the keyboard or mouse. Nevertheless, Visual Basic's controls just aren't enough to handle all the input that your program will need. Input boxes are great to use when the user must respond to certain kinds of questions. Text boxes and other controls are fine for getting fixed input from the user, such as data values with which the program will compute. Input boxes are great for asking the user questions that arise only under certain conditions. Input boxes always give the user a place to respond with an answer. In Figure 9.2, the input box is asking the user for a title that will go at the top of a printed report listing.<BR><P><B> <A HREF="09vel02.gif">Figure 9.2. Use input boxes to request information.</A></B><BR><P>Note that there is more than one way for the user to respond to Figure 9.2's input box. The user can answer the question by typing the title at the bottom of the input box and pressing Enter or clicking the OK command button. The user also can click the Cancel command button whether or not the user entered a title. Therefore, the program must be capable of reading the user's entered answer as well as responding to a Cancel command button press. Responding to message box and input box command buttons is part of the processing that you'll learn about in the remaining sections of this chapter.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Message boxes display output and input boxes get input. The message and input boxes offer ways for your programs to request information that regular controls can't handle. Use controls to display and get data values that are always needed. Use message and input boxes to display messages and get answers that the program needs in special cases, such as for error conditions and exception handling.<BR><BR><A NAME="E68E74"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>MsgBox</B><B> Statement</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The MsgBox statement displays messages for the user. In addition, the MsgBox() function displays messages but also provides a way for your program to display and check for multiple command button clicks on the message box window. This section explains how to use the MsgBox statement.<BR><P>All message boxes displayed with the MsgBox statement display an OK command button. The command button gives the user a chance to read the message. When the user has finished reading the message, the user can click OK to get rid of the message box. Your program suspends execution during the reading of the message box. Therefore, the statement following the MsgBox statement executes when the user clicks OK.<BR><P>Here is the format of the MsgBox statement:<BR><BR><PRE><FONT COLOR="#000080">MsgBox <I>msg</I> [, [<I>type</I>] [, <I>title</I>]]</FONT></PRE><P>The <I>msg</I> is a string (either variable or a string constant enclosed in quotation marks) and forms the text of the message displayed in the message box. The <I>type</I> is an optional numeric value or expression that describes the options you want in the message box. Figure 9.3 shows which icons you can place in a message box (Visual Basic displays no icon if you don't specify a <I>type</I> value). The <I>title</I> is an optional string that represents the text in the message box's title bar. If you omit the <I>title</I>, Visual Basic uses the project's name for the message box's title bar text.<BR><P><B> <A HREF="09vel03.gif">Figure 9.3. The icons that you can display in a message box.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Modality</I> determines whether the user or system responds to a message box.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The options that you select, using the <I>type</I> value in the MsgBox statement, determine whether the message box displays an icon as well as controls the modality of the message box. Table 9.1 contains the code values that you'll use to form the MsgBox <I>type</I> value.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 9.1. The MsgBox statement's </B><FONT COLOR="#FF8000"><B><I>type</I></B></FONT><B> values.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Value</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>CONSTANT.TXT Value</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>16</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_ICONSTOP</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the stop sign icon</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>32</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_ICONQUESTION</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the question mark icon</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>48</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_ICONEXCLAMATION</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the exclamation icon</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>64</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_ICONINFORMATION</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the lowercase i (meaning <I>information</I>) icon</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>4096</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_SYSTEMMODAL</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user's application is <I>system </I><I>modal</I>, meaning that the message box must be handled before you can switch to any other Windows program</FONT></TABLE><P>The modality often causes confusion. If you don't specify the system model <I>type</I> value of 4096 (or if you don't use CONSTANT.TXT's MB_SYSTEMMODAL named constant to specify the system's modal mode), the user's application won't continue until the user closes the message box, but the user <I>can</I> switch to another Windows program by pressing Alt+Tab or by switching to another program using the application's control menu. If, however, you do specify that the message box is system modal, the user won't able to switch to another Windows program until the user responds to the message box, because the message box will have full control of the system. Reserve the system modal message boxes for serious error messages that the user <I>must</I> read and respond to before continuing with the program.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>If you don't specify an icon, Visual Basic doesn't display an icon. If you don't specify the system modality, Visual Basic assumes that you want an application modal message box.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 9.2 ought to answer questions that you have about message boxes. The listing contains a series of message box statements. Each statement displays a different message box and each message box is different.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Use the MsgBox statement to display messages, such as error messages, to the user. The message boxes will contain an OK button that the user clicks to close the message box. Depending on whether you specify a <I>type</I> value, the message box might contain an icon as well as be system modal. If you specify a third <I>title</I> value, the message box's title bar will contain the text of the title you request.<BR><P><FONT COLOR="#000080"><B>Listing 9.2. Displaying different message boxes.</B></FONT><BR><PRE><FONT COLOR="#000080">1: ' A simple message box with a message and no icon2: MsgBox &quot;Just a message&quot;3: ' A message box with the stop sign icon4: MsgBox &quot;Stop in the name of love&quot;, MB_ICONSTOP5: ' A message box with the question mark icon6: MsgBox &quot;Did you turn on the printer?&quot;, MB_ICONQUESTION7: ' A message box with the exclamation icon8: MsgBox &quot;Don't forget to exit!&quot;, MB_ICONEXCLAMATION9: ' A message box with the &quot;i&quot; information icon as10: ' well as a title that's not the project name11: MsgBox &quot;A byte is 8 bits&quot;, MB_ICONINFORMATION, &quot;A title&quot;12: ' A message box that is system modal13: MsgBox &quot;You cannot switch programs&quot;, MB_SYSTEMMODAL14: ' A message box with a question mark icon, a system modal setting,15: ' and a title of Title16: MsgBox &quot;Info icon, system modal, and a title&quot;, MB_ICONQUESTION + MB_SYSTEMMODAL, &quot;Title&quot;</FONT></PRE><P>[ic:Output]Listing 9.2 demonstrates several different kinds of message boxes. Assuming that the project name is PROJECT1.MAK, Figure 9.4 shows the simple message that appears from line 2's MsgBox statement.<BR><P><B> <A HREF="09vel04.gif">Figure 9.4. A simple message box with no icon or given title.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Note that there is no icon because line 2 doesn't contain a <I>type</I> value. Also, the message box's title is Project1 because the project's name is PROJECT1.MAK and the MsgBox statement doesn't specify a different title. The message box also is application modal. The user must respond to the message before continuing with the program. The user can switch to another Windows program, however, by pressing Alt+Tab, or Alt+spacebar, or by selecting from the control menu.<BR><P>Line 4's MsgBox statement displays a stop sign icon to the left of the message. Rather than use the CONSTANT.TXT named constant, you can type 16 for the <I>type</I> value.<BR><P>Line 6's MsgBox statement displays a question mark icon to the left of the message. Line 8's MsgBox statement displays an exclamation point icon to the left of the message.<BR><P>Line 11 displays a message as well as the information icon. The information icon is a nice touch to add when offering advice to the user. Line 11 also displays a title in the message box's title bar. The title is simple: A title.<BR><P>Line 13's MsgBox produces a system modal message box. Line 14's MsgBox statement displays a message, an information icon, and a title, a title; this line is a system modal message box. Figure 9.5 shows Line 13's message box output.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR>

⌨️ 快捷键说明

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