vel09.htm

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

HTM
2,065
字号
<NOTE><B>Note: </B>Note that Visual Basic expands or contracts the message boxes to hold the full text that you want to display.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><B> <A HREF="09vel05.gif">Figure 9.5. A complete message box statement produces a message, icon, and </B><B>title.</A></B><BR><P>As you can see from Line 13's <I>type</I> value, you can add together an icon's value as well as the system modal value (or their named constants, as done in Listing 9.2) to obtain both an icon as well as a system modal message box.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>If you want to specify a title in a message box but <I>not</I> display an icon or change to system modality, insert two commas before the title string, as follows :<BR>MsgBox &quot;A byte is 8 bits&quot;, , &quot;A title&quot;</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E75"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The </B><B>MsgBox()</B><B> Function</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Use the MsgBox() function when you want the user to indicate a response to the message in the message box. By using the MsgBox() function, you can display several different command buttons inside the message box and determine which command button the user pressed so that you'll know how the user responded to the message.<BR><P>Figure 9.6 shows a message box that looks a little different from the other ones that you've seen so far. Instead of a single OK command button, Figure 9.6's message box contains three command buttons. The MsgBox() function enables you to program message boxes with multiple command buttons and then determine which command button the user pressed to close the message box.<BR><P><B> <A HREF="09vel06.gif">Figure 9.6. Message boxes can have several command buttons.</A></B><BR><P>The format of the MsgBox() function is almost identical to that of the MsgBox statement. You must use the MsgBox() function differently from the statement, however. Always assign a MsgBox() function to a variable. Here is the format of the MsgBox() function:<BR><BR><PRE><FONT COLOR="#000080">anIntVariable = MsgBox( msg [, [type] [, title]])</FONT></PRE><P>As you can see from the format, the MsgBox() function's format differs from the MsgBox statement in that you assign the MsgBox() function to an integer variable that you've already defined. In addition, the MsgBox() function supports several more <I>type</I> values than the MsgBox statement supported. Table 9.2 lists the MsgBox() function's <I>type</I> values along with their CONSTANT.TXT named constant equivalents.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 9.2. The MsgBox() function'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 </I><I>Value</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>0</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_OK</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The OK button appears only in the message box</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>1</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_OKCANCEL</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The OK and Cancel buttons appear</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>2</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_ABORTRETRYIGNORE</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The Abort, Retry, and Cancel buttons appear</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>3</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_YESNOCANCEL</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The Yes, No, and Cancel buttons appear</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>4</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_YESNO</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The Yes and No buttons appear</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>5</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_RETRYCANCEL</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The Retry and Cancel buttons appear</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>0</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_DEFBUTTON1</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The first button has the initial focus</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>256</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_DEFBUTTON2</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The second button has the initial focus</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>512</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_DEFBUTTON3</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The third button has the initial focus</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>4096</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MB_SYSTEMMODAL<I> </I><I>system</I> message</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user's application is<I> modal</I>, meaning that the box must be handled before you can switch to any other Windows program</FONT></TABLE><P>Here is the MsgBox() function that displayed the message box shown in Figure 9.6:<BR><BR><PRE><FONT COLOR="#000080">BPress = MsgBox(&quot;Are you ready for the report?&quot;, MB_ICONQUESTION + MB_YESNOCANCEL, &quot;Report Request&quot;)</FONT></PRE><P>The MB_ICONQUESTION named constant added to the MB_YESNOCANCEL named constant produced both a question mark icon and the three buttons. A title also appeared due to the third value inside the MsgBox() function.<BR><P>The reason that you assign MsgBox() functions to variables is so that you can tell what button the user pressed. Suppose that the user pressed the Yes button in Figure 9.6. The program could then print the report. If, however, the user pressed the No command button, the program could describe what the user needed to do to get ready for the report (load paper, turn on the printer, and so on). If the user pressed the Cancel command button, the program would know that the user didn't want the report at all. Of course, the application determines what set of command buttons will work best for any given message box.<BR><P>Table 9.3 lists the possible return values for the MsgBox() function. In other words, the integer variable will contain one of Table 9.3's values after every MsgBox() function completes. A subsequent If statement can then test to see which command button the user pressed.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 9.3. The </B><B>MsgBox()</B><B> function's return command button 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>1</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDOK</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the OK button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>2</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDCANCEL</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the Cancel button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>3</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>IDABORT</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The user pressed the Abort button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>4</FONT>

⌨️ 快捷键说明

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