Friday, October 21, 2011

MessageBox in VB.Net


You want to use the MessageBox.Show function in your VB.NET program that targets the Windows Forms application framework. The MessageBox.Show function immediately displays a dialog box in the interface and you can specify buttons, default buttons, an icon, and some other properties of the dialog. Here we look at a program text that has examples of the MessageBox.Show function being used in different ways, using the VB.NET language.
Examples of MessageBox.Show in Windows Forms.

Examples

First here we look at an event handler in the VB.NET language that will show the message boxes in the screenshot sequentially when the program executes. The MessageBox.Show function is a Public Shared Function type in the VB.NET language and it can be called without an instance reference. You can invoke MessageBox.Show directly. 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 in the VB.NET language 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.
Line continuation syntax used. The method invocations in the VB.NET program are very lengthy to type out and this would cause horizontal scrolling in the Visual Studio editor window. To fix this, you can use the underscore preceded by a space character at the end of each line. This allows you to use a multiple-line function call syntax.
MessageBox.Show overload descriptions. Here we summarize the MessageBox.Show invocations in this program. The Visual Studio IntelliSense feature is very useful for scrolling through all the possible function calls. The arguments specify the String types and the enumerated constants that dictate the appearance and behavior of the resulting message boxes. The overloads with one and two parameters are simplistic and have only one button (OK). The longer overloads have multiple buttons, captions, default buttons, and icons, and this only samples the features.

DialogResult

In this part, we note the DialogResult variables in the program text. 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 call. This is demonstrated in the program method text.

C#

Programming tip.
In this section, we mention that this article was adapted from the equivalent article on the C# usage of the MessageBox.Show method. The original C# article contains some material about the user interface considerations when using the MessageBox.Show function and this material is applicable to any VB.NET program. Other than this extra material, the articles are essentially equivalent except for syntax.
MessageBox.Show Examples in Windows Forms

Summary

We explored the MessageBox.Show function in the VB.NET programming language. We saw a program text in the VB.NET language that specifies an event handler method body and this called into the MessageBox.Show overloads, demonstrating how its behavior differs based on the arguments. We additionally noted the usage of the DialogResult enumeration and how to test the results of the MessageBox.Show function.
Windows Forms