MessageBox Dialog in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson explores MessageBox dialog and demonstrates how you can use it in your Visual Basic applications.

MessageBox is one of these built-in dialog boxes that help you to provide a rich user interface in your front-end applications.

As a developer you will use this dialog pretty often as it lets you to display custom messages to your users and accept their input regarding the choice that they have made.

It's very flexible and provide many properties and methods that enable you to customize them to suit your needs.

You can customize it to display a variety of icons with your messages and choose which buttons to display while still maintaining the standard look of Windows Forms application.

The MessageBox suports the following standard icons:

MEMBER ICON
Asterisk Information Icon
Information Information Icon
Error Error Icon
Hand Error Icon
Stop Error Icon
Exclamation Exclamation Icon
Warning Exclamation Icon
Question Question Icon
None Will not display any icon

It also supports the following combinations of buttons:

MEMBER DESCRIPTION
AbortRetryIgnore Abort, Retry, and Ignore buttons
OK an OK button
OKCancel OK and Cancel buttons
RetryCancel Retry and Cancel buttons
YesNo Yes and No buttons
YesNoCancel Yes, No, and Cancel buttons

To display the message box you call its Show method which by the way, cannot be inherited because it's shared so the only way to change the default MessageBox behaviour is using windows hooks.

    MessageBox.Show( "The username or password you entered is incorrect", _
        "Login Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop)                   


Get the message box response clicked by user:

You can get the response of the message box clicked by the user (Yes, No, Cancel, etc) by using the following code:

	Dim msgRslt As MsgBoxResult = MsgBox("Are you 21 years old or older?.", MsgBoxStyle.YesNo)
        If msgRslt = MsgBoxResult.Yes Then
            MsgBox("You are welcome to join.")
        ElseIf msgRslt = MsgBoxResult.No Then
            MsgBox("You must be at least 21 years old to join.")
    End If
Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.