MessageBox.Show
This VB.NET function displays a dialog box. It interrupts the user, and immediately blocks further interaction. Only one function call is needed.
This tutorial begins with many different calls to MessageBox.Show
. You can find the one that matches what you need, and use the syntax.
This event handler will show the message boxes. The MessageBox.Show
function is a Public Shared Function type. It can be called without an instance reference.
MessageBox.Show
. The method receives different numbers of parameters.MessageBox.Show
calls into the Form1
constructor. In your programs, place MessageBox.Show
where a dialog is needed.Public Class Form1 Public Sub New() ' Use 1 argument. MessageBox.Show("Dot Net Perls is awesome.") ' Use 2 arguments. MessageBox.Show("Dot Net Perls is awesome.", "Important Message") ' Use 3 arguments. Dim result1 As DialogResult = MessageBox.Show("Is Dot Net Perls awesome?", "Important Question", MessageBoxButtons.YesNo) ' Use 4 arguments. Dim result2 As DialogResult = MessageBox.Show("Is Dot Net Perls awesome?", "Important Query", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) ' Use 5 arguments. Dim result3 As DialogResult = MessageBox.Show("Is this website awesome?", "The Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) ' Test DialogResult. If result1 = DialogResult.Yes And result2 = DialogResult.Yes And result3 = DialogResult.No Then MessageBox.Show("You answered yes, yes and no.") End If ' Use 7 arguments. MessageBox.Show("Dot Net Perls is the best.", "Critical Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, True) End Sub End Class
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.
Public Class Form1 Public Sub New() ' Use 1 argument. MessageBox.Show() ' ... End Sub End Class
Add the statements in the example to show dialog boxes when the program is executed. The methods might cause horizontal scrolling in the Visual Studio editor window.
DialogResult
Let us consider the Dialog Results in the program. You can use a Dim
variable As DialogResult
and then assign this to the result of MessageBox.Show
.
We explored the MessageBox.Show
function. We called into the MessageBox.Show
overloads, and noted the usage of the DialogResult
type.