Home
VB.NET.WinForms
MessageBox.Show Examples
Updated 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.
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.
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.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 27, 2021 (edit).
Home
Changes
© 2007-2025 Sam Allen