MessageBox.Show Examples
This page was last reviewed on May 27, 2021.
Dot Net Perls
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.
Shows a messageboxShows a messagebox
MessageBox notes. This tutorial begins with many different calls to MessageBox.Show. You can find the one that matches what you need, and use the syntax.
Some examples. 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.
Tip You can directly invoke MessageBox.Show. The method receives different numbers of parameters.
Here We placed the MessageBox.Show calls into the Form1 constructor. In your programs, place MessageBox.Show where a dialog is needed.
Shows a messagebox
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
Visual Studio. 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.
Shows a messagebox
Public Class Form1 Public Sub New() ' Use 1 argument. MessageBox.Show() ' ... End Sub End Class
Notes, continued. 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.
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.
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.
A summary. We explored the MessageBox.Show function. We called into the MessageBox.Show overloads, and noted the usage of the DialogResult type.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 27, 2021 (edit).
© 2007-2024 Sam Allen.