TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET MessageBox.Show Examples

Use the MessageBox.Show function in Windows Forms to display a dialog box with OK and Cancel buttons.
MessageBox.Show. This displays a dialog box. It interrupts the user. It immediately blocks further interaction. It requires only one Function call.
A brief introduction. This tutorial begins with 8 different calls to MessageBox.Show. You can find the one that matches what you need, and use the syntax.
Example 1. We specify buttons, default buttons, an icon, and some other properties of the dialog. Here is a simple (and silly) example.
VB.NET program that uses single argument ' ' First show a single-argument dialog box with MessageBox.Show. ' MessageBox.Show("The Dev Codes is awesome.")
Example 2. Here we see a title has appeared—this is added with another argument to the MessageBox.Show method. We add arguments to change the dialog.
VB.NET program that uses two arguments ' ' Show a two-argument dialog box with this method. ' MessageBox.Show("The Dev Codes is awesome.", _ "Important Message")
Example 3. Here is a dialog with two buttons. We can use DialogResult to detect the result of the button clicked (or indicated with a key press).
VB.NET program that uses 3 arguments ' ' 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 The Dev Codes awesome?", _ "Important Question", _ MessageBoxButtons.YesNo)
Example 4. We see a third button. And we add an icon to the dialog box also. Sorry if this is confusing. Once you have a working code example, it is easy to modify.

Tip: Use IntelliSense in Visual Studio when you type MessageBox.Show to see a list of the possible function overloads.

VB.NET program that uses 4 arguments ' ' Use four parameters with the method. ' ... Use the YesNoCancel enumerated constant and the Question icon. ' Dim result2 As DialogResult = MessageBox.Show("Is The Dev Codes awesome?", _ "Important Query", _ MessageBoxButtons.YesNoCancel, _ MessageBoxIcon.Question)
Example 5. I like Visual Basic, but it is not the most popular language. We change the default but to ninth is dialog to be the answer "No."
VB.NET program that uses 5 arguments ' ' 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)
Example 6. Dialog can display the results also. Here we test for the DialogResult.Yes and No enumerations. We display the values.If Then
VB.NET program that tests DialogResult ' ' 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
Example 7. Alignment is also possible. This is not the most useful feature, but like the others it can be specified with an enumerated value. Please see the matching VB.NET code.

Note: Sorry about the excessive self-promotion in the dialog boxes—you can change them to promote yourself instead.

VB.NET program that uses RightAlign ' ' Use MessageBox.Show overload that has seven arguments. ' MessageBox.Show("The Dev Codes is the best.", _ "Critical Warning", _ MessageBoxButtons.OKCancel, _ MessageBoxIcon.Warning, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.RightAlign, _ True)
Example 8. Here we use the MessageBoxIcon.Exclamation enum. The program renders an exclamation icon. This is included in Windows Forms.
VB.NET program that uses MessageBoxIcon ' ' Show a dialog box with a single button. ' MessageBox.Show("The Dev Codes is super.", _ "Important Note", _ MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, _ MessageBoxDefaultButton.Button1)
Code for windows. 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.Shared

Tip: You can directly invoke MessageBox.Show. The method receives different numbers of parameters.

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("The Dev Codes is awesome.") ' ' Show a two-argument dialog box with this method. ' MessageBox.Show("The Dev Codes 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 The Dev Codes 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 The Dev Codes 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("The Dev Codes is the best.", _ "Critical Warning", _ MessageBoxButtons.OKCancel, _ MessageBoxIcon.Warning, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.RightAlign, _ True) ' ' Show a dialog box with a single button. ' MessageBox.Show("The Dev Codes is super.", _ "Important Note", _ MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, _ MessageBoxDefaultButton.Button1) End Sub End Class
Notes, program. It contains statements in the Form1_Load event handler. To add the load event handler, double-click anywhere in the window in the Visual Studio designer.
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.

Sub
A note. 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.

DialogResult. Let's consider the DialogResults in the program. You can use a Dim variable As DialogResult and then assign this to the result of MessageBox.Show.

Note: This is demonstrated in the program. Usage of the DialogResult type is an important concept.

A summary. We explored the MessageBox.Show function. We called into the MessageBox.Show overloads. We noted the usage of the DialogResult.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf