Visual Basic Functions, The Message Box

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Introduction

A message box is a special dialog box used to display a piece of information to the user. As opposed to a
regular form, the user cannot type anything in the dialog box. To support message boxes, the Visual Basic
language provides a function named MsgBox. To support message boxes, the .NET Framework provides a
class named.

To display a simple message box, you can use the MsgBox() function with the following formula:

MsgBox(Message)

Inside the parentheses, pass a string. Here is an example:


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

If the message is made of different sections, you can concatenate them using the & operator. You can also first declare a Str
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 is simply a
preference; but it is also because these lessons are for Microsoft Visual 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 by clicking a button and, dependin
the button the user would have clicked, the message box would return a value. To be able to return a value, the MsgBox()
function is declared 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
of the MsgBox() function is based on the MsgBoxResult enumeration. The buttons and the returned values are as follows:

If the User Clicks Button Caption Integral Value


OK 1
Cancel 2
Abort 3
Retry 4
Ignore 5
Yes 6
No 7
The Buttons of a Message Box

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

Integral
To Display MsgBoxStyle
Value
OKOnly 0
OKCancel 1

AbortRetryIgnore 2

YesNoCancel 3

YesNo 4

RetryCancel 5

To use any of these combinations of buttons, call the MessageBoxStyle enumeration and access the desired combination. H
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:


The Caption of a Message Box

If you create a simple message box by providing only the message, the dialog box would appear with the name of the projec
the title. To allow you to specify a caption of your choice, provide a second string as the third 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:

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 enumeratio
provides the following additional members:

Integral
To Display MsgBoxStyle
Value
Critical 16

Question 32

Exclamation 48

Information 64

To apply one of these buttons, combine its style with that of 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:

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
would behave as if the user had clicked the default button. If the message box has more than one button, you can decide wha
button would be the default. To support the default button, the MsgBoxStyle enumeration provides the following additional
options:

If the message box contains


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

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 Or _
MsgBoxStyle.DefaultButton2, _
"Customer Order Processing")
End Sub

You might also like