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
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 |
1 |
|
Cancel |
2 |
|
Abort |
3 |
|
Retry |
4 |
|
Ignore |
5 |
|
Yes |
6 |
|
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 |
 |
OKOnly |
0 |
 |
OKCancel |
1 |
 |
AbortRetryIgnore |
2 |
 |
YesNoCancel |
3 |
|
YesNo |
4 |
 |
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:
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:
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 |
16 |
 |
 |
Question |
32 |
 |
 |
Exclamation |
48 |
 |
 |
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:
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
|