Examining MsgBox()Always assign a MsgBox() function to an integer variable. The variable will hold the return value, and that value will indicate the button the user clicked (message boxes can display multiple buttons such as OK and Cancel). Here is the format of the MsgBox() function: anIntVariable = MsgBox( strMsg [, [intType] [, strTitle]])
strMsg is a string (either a variable or a string constant enclosed in quotation marks) and forms the text of the message displayed in the message box. intType is an optional numeric value or expression that describes the options you want in the message box. Table 6.1, Table 6.2, and Table 6.3 contain all the possible values you can use for the type of message box you want displayed. (Visual Basic displays no icon if you don't specify an intType value.) If you want to use a value from two or more of the tables, you'll add the values together. Although you can use the integer value, if you use the built-in Visual Basic named literal, you'll more easily understand the message box's style if you ever have to change the message box in the future. strTitle is an optional string that represents the text in the message box's title bar. If you omit strTitle, Visual Basic uses the project's name for the message box's title bar text. Table 6.1. Select the buttons displayed in the message box.
Table 6.2. Select the icon displayed in the message box.
Table 6.3. Select the default button in the message box.
The options that you select using the intType value in the MsgBox() function determine whether the message box displays an icon and controls the modality of the message box. The modality determines whether a message box is application-specific or system-specific. If it's application-specific, the user must respond to the message box before doing anything else in the application. If the message box is system-specific, the user must respond to the message box before doing anything else on the system.
The modality often causes confusion. If you don't specify a system-modal intType value of 4096 (or if you don't use the named literal vbSystemModal to specify the system's modal mode), the user's application will not continue until the user closes the message box. However, the user can switch to another Windows program by pressing Alt+Tab or using the application's control menu. If, however, you do specify that the message box is system modal, the user will not be 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 you want the user to read and respond to before continuing the program. You may make a message box a System Modal message box by adding 4096 to the intType value of the message box.
Remember that the MsgBox() values such as vbQuestion and vbYesNoCancel are not variables but named literals that Visual Basic has defined to correspond with matching integer values. The named literals vbQuestion and vbYesNoCancel produced both a question mark icon and the three buttons. A title also appeared due to the third value inside the MsgBox() function. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||