home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The MsgBox Function

The MsgBox function is one of Visual Basic’s most useful tools. You use it to display brief messages to the user and get a response. In its simplest form, MsgBox displays your message in a small dialog box containing an OK button. The title of the dialog box is the same as the project name. For example, the statement

MsgBox(“This is the message”)

displays the box shown in Figure 11.5. Program execution pauses while the box is displayed and continues once the user has selected OK.

The MsgBox function is a great deal more flexible than this, however. You can specify your own title as well as the display of different combinations of buttons (Yes and No buttons, for example). When more than one button is displayed, the return value of the MsgBox function indicates which button the user selected. You can also have a graphical icon, such as a question mark, displayed in the message box. Here is the full syntax of the MsgBox function:

return = MsgBox(message, flags, title)

Let’s take a brief tour of the arguments:

  message—The string literal or variable specifying the message to be displayed
  title—The string literal or variable specifying the message box title
  flags—An integer value that controls the display of buttons and icons


Figure 11.5  A basic message box.

Table 11.2 Predefined constants for the MsgBox function’s flags argument.

Constant Value Description
vbOKCancel 1 Display OK and Cancel buttons
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons
vbYesNoCancel 3 Display Yes, No, and Cancel buttons
vbYesNo 4 Display Yes and No buttons
vbRetryCancel 5 Display Retry and Cancel buttons
vbCritical 16 Display Critical Message icon
vbQuestion 32 Display Warning Query icon
vbExclamation 48 Display Warning Message icon
vbInformation 64 Display Information Message icon
vbDefaultButton 2256 Second button is default
vbDefaultButton 3512 Third button is default

For the flags argument, you can use the predefined constants listed in Table 11.2. The last two constants determine which button is the default—that is, which one will be selected if the user simply presses Enter. The normal default is the first button. To combine constants—to specify buttons and an icon, for example—use the Or operator. The following call would display the Abort, Retry, and Ignore buttons, an exclamation icon, and make the Retry button the default:

x = MsgBox(“Message Here”, vbAbortRetryIgnore Or vbExclamation Or _
           vbDefaultButton2, “Title Here”)

The possible return values are also defined by constants, which are listed in Table 11.3.

Table 11.3 Predefined constants for return values of the flags argument.

Constant Value Button Chosen
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No

Creating The SaveChanges Function

Go ahead now and create the SaveChanges function, assigning it a return type of Boolean. The function code is shown in Listing 11.14. Note that the code does not explicitly test for a return from the message box of vbNo. If the user selects No (the only possibility besides Yes and Cancel), execution falls through the If block, and the function terminates with a return value of True.

Listing 11.14 The SaveChanges function.

Private Function SaveChanges() As Boolean

‘ Determines if the text being edited has changed since the
‘ last File|Save command. If so, the function offers the user the option
‘ of saving the file.
‘ Function returns True if there have been no changes or if
‘ the user saves the changes; returns False if the user
‘ selects Cancel.

Dim Title As String, Msg As String
Dim Reply As Integer, Flags As Integer

If TextChanged = True Then
    Title = “Text has changed”
    Msg = “Save changes to text?”
    Flags = vbYESNOCANCEL + vbQUESTION
    Reply = MsgBox(Msg, Flags, Title)

    If Reply = vbYES Then
        Call SaveFile
    ElseIf Reply = vbCANCEL Then
        SaveChanges = False
        Exit Function
    End If
End If

SaveChanges = True

End Function


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.