Visual Basic Functions: The Message Box (2024)

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MsgBox("Welcome to Microsoft Visual Basic")End Sub
Visual Basic Functions: The Message Box (1)

If the message is made of different sections, you can concatenate them using the & operator. You can also first declare a String variable, initialize it, and pass it to the function.

To create a message box using the .NET Framework, you can call the Show() method of the MessageBox class using the following formula:

MessageBox.Show(Message)

As done for the MsgBox() function, pass a string to the method. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MessageBox.Show("Welcome to Microsoft Visual Basic")End Sub

https://demoslotsgames.com/en/ showcases a vivid and colorful world where fruits and candies blend harmoniously with rewarding bonus rounds. It's not only about fun; it's also a learning experience for those curious about the game mechanics.

In our lessons, we will mostly use the MsgBox()function, not because it is better than the MessageBox class. It issimply a preference; but it is also because these lessons are for MicrosoftVisual Basic, so we give preference to its own (rich) library.

The Return Value of a Message Box

Besides displaying a message, a message box can be used to let the user make a decision byclicking a button and, depending on the button the user would have clicked, the messagebox would return a value. To be able to return a value, the MsgBox() function isdeclared as follows:

Public Shared Function MsgBox ( _Prompt As Object, _<OptionalAttribute> Optional Buttons As MsgBoxStyle = MsgBoxStyle.OkOnly, _<OptionalAttribute> Optional Title As Object = Nothing _) As MsgBoxResult

The value returned by a message box corresponds to a button the user would have clicked (on the message box). The return value of theMsgBox() function is based on the MsgBoxResult enumeration. The buttons and the returned values are as follows:

If the User Clicks Button Caption Integral Value
Visual Basic Functions: The Message Box (2) OK 1
Visual Basic Functions: The Message Box (3) Cancel 2
Visual Basic Functions: The Message Box (4) Abort 3
Visual Basic Functions: The Message Box (5) Retry 4
Visual Basic Functions: The Message Box (6) Ignore 5
Visual Basic Functions: The Message Box (7) Yes 6
Visual Basic Functions: The Message Box (8) No 7

The Buttons of a Message Box

If you create a simple message box by providing only themessage, it would appear with only one button labeled OK. If you want the userto be able to make a decision and communicate it to you, provide a secondargument. The second argument must be based on the MsgBoxStyleenumeration. When it comes to buttons, some members of this enumeration are:

To Display MsgBoxStyle Integral Value
Visual Basic Functions: The Message Box (9) OKOnly 0
Visual Basic Functions: The Message Box (10) Visual Basic Functions: The Message Box (11) OKCancel 1
Visual Basic Functions: The Message Box (12) Visual Basic Functions: The Message Box (13) Visual Basic Functions: The Message Box (14) AbortRetryIgnore 2
Visual Basic Functions: The Message Box (15) Visual Basic Functions: The Message Box (16) Visual Basic Functions: The Message Box (17) YesNoCancel 3
Visual Basic Functions: The Message Box (18) Visual Basic Functions: The Message Box (19) YesNo 4
Visual Basic Functions: The Message Box (20) Visual Basic Functions: The Message Box (21) RetryCancel 5

To use any of these combinations of buttons, call the MessageBoxStyle enumeration and access the desired combination. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MsgBox("Now we will move to the next step", MsgBoxStyle.OkCancel)End Sub

This would produce:

Visual Basic Functions: The Message Box (22)

The Caption of a Message Box

If you create a simple message box by providing only themessage, the dialog box would appear with the name of the project in the title.To allow you to specify a caption of your choice, provide a second string as thethird argument to the MsgBox() function. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MsgBox("Now we will move to the next step", _ MsgBoxStyle.OkCancel, "Lessons Objectives")End Sub

This would produce:

Visual Basic Functions: The Message Box (23)

The Icon of a Message Box

To enhance the appearance of a message box, you can display an icon on it. To support icons, the MsgBoxStyle enumeration provides the following additional members:

To Display MsgBoxStyle Integral Value
Visual Basic Functions: The Message Box (24) Visual Basic Functions: The Message Box (25) Critical 16
Visual Basic Functions: The Message Box (26) Visual Basic Functions: The Message Box (27) Question 32
Visual Basic Functions: The Message Box (28) Visual Basic Functions: The Message Box (29) Exclamation 48
Visual Basic Functions: The Message Box (30) Visual Basic Functions: The Message Box (31) Information 64

To apply one of these buttons, combine its style with thatof the button, using the OR operator. Here is an example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MsgBox("Are you ready to provide your credit card information?", _ MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question, _ "Customer Order Processing")End Sub

This would produce:

Visual Basic Functions: The Message Box (32)

The Default Button of a Message Box

When a message box is configured to display more than one button, the operating system is set to decide which button is the default. The default button has a thick border that sets it apart from the other button(s). If the user presses Enter, the message box would behave as if the user had clicked the default button. If the message box has more than one button, you can decide what button would be the default. Tosupport the default button, the MsgBoxStyle enumeration provides the following additionaloptions:

MsgBoxStyle Integral Value If the message box contains more than one button, the default button would be
DefaultButton1 0 the first
DefaultButton2 256 the second
DefaultButton3 512 the third

Here isan example:

Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click MsgBox("Are you ready to provide your credit card information?", _ MsgBoxStyle.YesNoCancel Or _ MsgBoxStyle.Question Or _ MsgBoxStyle.DefaultButton2, _ "Customer Order Processing")End Sub
Visual Basic Functions: The Message Box (33)
Visual Basic Functions: The Message Box (2024)
Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5661

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.