MessageBox.Show displays a dialog box. It interrupts the user. It immediately blocks further interaction. It requires only one Function call. We specify buttons, default buttons, an icon, and some other properties of the dialog.
First, we look at an event handler that will show the message boxes in the screenshot sequentially when the program executes. The MessageBox.Show function is a Public Shared Function type. It can be called without an instance reference.Shared
You can directly invoke MessageBox.Show. The method receives different numbers of parameters, and by specifying certain arguments and the right number of arguments, you can get the desired dialog box.
Event handler function that uses MessageBox.Show: VB.NET Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' ' First show a single-argument dialog box with MessageBox.Show. ' MessageBox.Show("Dot Net Perls is awesome.") ' ' Show a two-argument dialog box with this method. ' MessageBox.Show("Dot Net Perls is awesome.", _ "Important Message") ' ' Use a three-argument dialog box with MessageBox.Show. ' ... Also store the result value in a variable slot. ' Dim result1 As DialogResult = MessageBox.Show("Is Dot Net Perls awesome?", _ "Important Question", _ MessageBoxButtons.YesNo) ' ' Use four parameters with the method. ' ... Use the YesNoCancel enumerated constant and the Question icon. ' Dim result2 As DialogResult = MessageBox.Show("Is Dot Net Perls awesome?", _ "Important Query", _ MessageBoxButtons.YesNoCancel, _ MessageBoxIcon.Question) ' ' Use five arguments on the method. ' ... This asks a question and you can test the result using the variable. ' Dim result3 As DialogResult = MessageBox.Show("Is Visual Basic awesome?", _ "The Question", _ MessageBoxButtons.YesNoCancel, _ MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button2) ' ' Use if-statement with dialog result. ' If result1 = DialogResult.Yes And _ result2 = DialogResult.Yes And _ result3 = DialogResult.No Then MessageBox.Show("You answered yes, yes and no.") ' Another dialog. End If ' ' Use MessageBox.Show overload that has seven arguments. ' MessageBox.Show("Dot Net Perls is the best.", _ "Critical Warning", _ MessageBoxButtons.OKCancel, _ MessageBoxIcon.Warning, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.RightAlign, _ True) ' ' Show a dialog box with a single button. ' MessageBox.Show("Dot Net Perls is super.", _ "Important Note", _ MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, _ MessageBoxDefaultButton.Button1) End Sub End Class
Using Form1_Load event handler. This example program contains statements in the Form1_Load event handler. To add the load event handler, you can double-click anywhere in the window inside your program in the Visual Studio designer.
Then, you can add the statements in the example to show dialog boxes when the program is executed. The method invocations are lengthy to type out and this would cause horizontal scrolling in the Visual Studio editor window.
Tip: To fix this, you can use the underscore preceded by a space character at the end of each line.
And: This allows you to use a multiple-line function call syntax. Newer versions of VB.NET are more relaxed in newline handling.Underscores
Here we summarize the MessageBox.Show invocations. The IntelliSense feature is useful for scrolling through all the possible function calls. The enumerated constants dictate the appearance and behavior of the message boxes.
Note: The overloads with one and two parameters are simplistic and have only one button (OK).
Note 2: The longer overloads have multiple buttons, captions, default buttons, and icons, and this only samples the features.
Let's consider the DialogResults in the program. You can use a Dim variable As DialogResult and then assign this to the result of the MessageBox.Show method invocation. Then, you can use an if-statement to test the result of the method.If Then
Note: This is demonstrated in the program. Usage of the DialogResult type is an important concept.
We explored the MessageBox.Show function. We saw a VB.NET program that specifies an event handler method body and this called into the MessageBox.Show overloads, demonstrating how its behavior differs based on the arguments.
Also: We noted the usage of the DialogResult enumeration and how to test the results of the MessageBox.Show function.