![]() ![]() | ||
In addition to the MsgBox function (see the previous topic), you can use the .NET framework's MessageBox class's Show method to display message boxes. This method has many overloaded forms; here's one of them:
Overloads Public Shared Function Show(ByVal text As String, _ ByVal caption As String, ByVal buttons As MessageBoxButtons, _ ByVal icon As MessageBoxIcon, ByVal defaultButton As _ MessageBoxDefaultButton, ByVal options As MessageBoxOptions _) As DialogResult
Here are the arguments you pass to this method:
text—The text to display in the message box.
caption—The text to display in the title bar of the message box.
buttons—One of the MessageBoxButtons enumeration values that specifies which buttons to display in the message box. See below.
icon—One of the MessageBoxIcon enumeration values that specifies which icon to display in the message box. See below.
defaultButton—One of the MessageBoxDefaultButton enumeration values that specifies which is the default button for the message box. See below.
options—One of the MessageBoxOptions enumeration values that specifies which display and association options will be used for the message box. See below.
Here are the MessageBoxButtons enumeration values:
AbortRetryIgnore— The message box will show Abort, Retry, and Ignore buttons.
OK— The message box will show an OK button.
OKCancel— The message box will show OK and Cancel buttons.
RetryCancel— The message box will show Retry and Cancel buttons.
YesNo— The message box will show Yes and No buttons.
YesNoCancel— The message box will show Yes, No, and Cancel buttons.
Here are the MessageBoxIcon enumeration values:
Asterisk— Shows an icon displaying a lowercase letter i in a circle.
Error— Shows an icon displaying a white X in a circle with a red background.
Exclamation— Shows an icon displaying an exclamation point in a triangle with a yellow background.
Hand— Shows an icon displaying a white X in a circle with a red background.
Information— Shows an icon displaying a lowercase letter i in a circle.
None— Shows no icons.
Question— Shows an icon displaying a question mark in a circle.
Stop— Shows an icon displaying white X in a circle with a red background.
Warning— Shows an icon displaying an exclamation point in a triangle with a yellow background.
Here are the MessageBoxDefaultButton enumeration values:
Button1— Makes the first button on the message box the default button.
Button2— Makes the second button on the message box the default button.
Button3— Makes the third button on the message box the default button.
Here are the MessageBoxOptions enumeration values:
DefaultDesktopOnly— Displays the message box on the active desktop.
RightAlign— The message box text is right-aligned.
RtlReading— Specifies that the message box text is displayed with right to left reading order.
The result of the Show method is a value from the DialogResult enumeration, showing what button the user clicked:
Abort— Returns Abort.
Cancel— Returns Cancel.
Ignore— Returns Ignore.
No— Returns No.
None— Nothing is returned from the dialog box. (Note that this means that a modal dialog continues running.)
OK— Returns OK.
Retry— Returns Retry.
Yes— Returns Yes.
Here's an example putting this to work, from the MsgAndInputBoxes example on the CD-ROM. Note that I'm testing the returned result to see if the user clicked the OK button:
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim Result As Integer Result = MessageBox.Show("This is a message box!", "Message Box", _ MessageBoxButtons.OKCancel, MessageBoxIcon.Information, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.DefaultDesktopOnly) If (Result = DialogResult.OK) Then TextBox1.Text = "You clicked OK" End If End Sub
You can see the results of this code in Figure 4.6. Note that this message box looks just like the one created with the MsgBox function in the previous topic.
![]() ![]() | ||