Tuesday, April 26, 2011

The Messagebox









 
      One of the best functions in Visual Basic is the message box.  The message box displays a message, optional icon, and selected set of command buttons.  The user responds by clicking a button.

     The statement form of the message box returns no value (it simply displays the box):
                 
MsgBox (Message, Type, Title)

where

Message        Text message to be displayed
Type               Type of message box (discussed in a bit)
Title                 Text in title bar of message box

You have no control over where the message box appears on the screen.

      The function form of the message box returns an integer value (corresponding to the button clicked by the user).  Example of use (Response is returned value):

Dim Response as Integer
Response = MsgBox(Message, Type, Title)

·         The Type argument is formed by summing four values corresponding to the buttons to display, any icon to show, which button is the default response, and the modality of the message box.
·         The first component of the Type value specifies the buttons to display:

Value    Meaning                                  Symbolic Constant
0             OK button only                       vbOKOnly
1             OK/Cancel buttons               vbOKCancel
2             Abort/Retry/Ignore buttons   vbAbortRetryIgnore
3             Yes/No/Cancel buttons        vbYesNoCancel
4             Yes/No buttons                      vbYesNo
5             Retry/Cancel buttons            vbRetryCancel

·         The second component of Type specifies the icon to display in the message box:

Value    Meaning                                  Symbolic Constant
0             No icon                                    (None)
16           Critical icon                            vbCritical
32           Question mark                       vbQuestion
48           Exclamation point                 vbExclamation
64           Information icon                    vbInformation

      The third component of Type specifies which button is default (i.e. pressing Enter is the same as clicking the default button):

Value    Meaning                                  Symbolic Constant
0             First button default                vbDefaultButton1
256        Second button default          vbDefaultButton2
512        Third button default              vbDefaultButton3

      The fourth and final component of Type specifies the modality:

Value    Meaning                                  Symbolic Constant
0             Application modal                 vbApplicationModal
4096      System modal                        vbSystemModal

If the box is Application Modal, the user must respond to the box before continuing work in the current application.  If the box is System Modal, all applications are suspended until the user responds to the message box.

      Note for each option in Type, there are numeric values listed and symbolic constants.  Recall, it is strongly suggested that the symbolic constants be used instead of the numeric values.  You should agree that vbOKOnly means more than the number 0 when selecting the button type.

      The value returned by the function form of the message box is related to the button clicked:

Value    Meaning                                  Symbolic Constant
1             OK button selected               vbOK
2             Cancel button selected        vbCancel
3             Abort button selected            vbAbort
4             Retry button selected            vbRetry
5             Ignore button selected          vbIgnore
6             Yes button selected              vbYes
7             No button selected                vbNo


·         Message Box Example:


 .

No comments:

Post a Comment