Home

Visual Basic Functions: The Message Box

 

Introduction

A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything in the dialog box. To support message boxes, the Visual Basic language provides a function named MsgBox. To support message boxes, the .NET Framework provides a class named.

To display a simple message box, you can use the MsgBox() function with the following formula:

MsgBox(Message)

Inside the parentheses, pass a string. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MsgBox("Welcome to Microsoft Visual Basic")
End Sub

Message Box

If the message is made of different sections, you can concatenate them using the & operator. You can also first declare a String variable, initialize it, and pass it to the function.

To create a message box using the .NET Framework, you can call the Show() method of the MessageBox class using the following formula:

MessageBox.Show(Message)

As done for the MsgBox() function, pass a string to the method. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MessageBox.Show("Welcome to Microsoft Visual Basic")
End Sub

In our lessons, we will mostly use the MsgBox() function, not because it is better than the MessageBox class. It is simply a preference; but it is also because these lessons are for Microsoft Visual Basic, so we give preference to its own (rich) library.

The Return Value of a Message Box

Besides displaying a message, a message box can be used to let the user make a decision by clicking a button and, depending on the button the user would have clicked, the message box would return a value. To be able to return a value, the MsgBox() function is declared as follows:

Public Shared Function MsgBox ( _
	Prompt As Object, _
	<OptionalAttribute> Optional Buttons As MsgBoxStyle = MsgBoxStyle.OkOnly, _
	<OptionalAttribute> Optional Title As Object = Nothing _
) As MsgBoxResult

The value returned by a message box corresponds to a button the user would have clicked (on the message box). The return value of the MsgBox() function is based on the MsgBoxResult enumeration. The buttons and the returned values are as follows:

If the User Clicks Button Caption Integral Value
OK OK 1
Cancel Cancel 2
Abort Abort 3
Retry Retry 4
Ignore Ignore 5
Yes Yes 6
No No 7

The Buttons of a Message Box

If you create a simple message box by providing only the message, it would appear with only one button labeled OK. If you want the user to be able to make a decision and communicate it to you, provide a second argument. The second argument must be based on the MsgBoxStyle enumeration. When it comes to buttons, some members of this enumeration are:

To Display MsgBoxStyle Integral Value
OK OKOnly 0
OK Cancel OKCancel 1
Abort Retry Ignore AbortRetryIgnore 2
Yes No Cancel YesNoCancel 3
Yes No YesNo 4
Retry Cancel RetryCancel 5

To use any of these combinations of buttons, call the MessageBoxStyle enumeration and access the desired combination. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MsgBox("Now we will move to the next step", MsgBoxStyle.OkCancel)
End Sub

This would produce:

Message Box With Buttons

 

The Caption of a Message Box

If you create a simple message box by providing only the message, the dialog box would appear with the name of the project in the title. To allow you to specify a caption of your choice, provide a second string as the third argument to the MsgBox() function. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MsgBox("Now we will move to the next step", _
                     MsgBoxStyle.OkCancel, "Lessons Objectives")
End Sub

This would produce:

Message Box

The Icon of a Message Box

To enhance the appearance of a message box, you can display an icon on it. To support icons, the MsgBoxStyle enumeration provides the following additional members:

To Display MsgBoxStyle Integral Value
Critical Critical 16 
Question Question 32
Exclamation Exclamation 48
Information Information 64

To apply one of these buttons, combine its style with that of the button, using the OR operator. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MsgBox("Are you ready to provide your credit card information?", _
               MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question, _
               "Customer Order Processing")
End Sub

This would produce:

Message Box With an Icon

The Default Button of a Message Box

When a message box is configured to display more than one button, the operating system is set to decide which button is the default. The default button has a thick border that sets it apart from the other button(s). If the user presses Enter, the message box would behave as if the user had clicked the default button. If the message box has more than one button, you can decide what button would be the default. To support the default button, the MsgBoxStyle enumeration provides the following additional options:

MsgBoxStyle Integral Value If the message box contains more than one button, the default button would be
DefaultButton1 0 the first
DefaultButton2 256 the second
DefaultButton3 512 the third

Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles btnMessage.Click
        MsgBox("Are you ready to provide your credit card information?", _
               MsgBoxStyle.YesNoCancel Or _
               MsgBoxStyle.Question Or _
               MsgBoxStyle.DefaultButton2, _
               "Customer Order Processing")
End Sub

Message Box With an Icon

 

 

 

Home Copyright © 2008-2012 FunctionX Home