X



Home>Tutorial>

Visual Basic MsgBox, MessageBox and InputBox Made Easy

Nuelson Penuel Wednesday, September 9, 2020 Visual Basic.Net

Summary

In this article, am going to show you what MsgBox, MessageBox.Show is, purpose of MsgBox and InputBox as well as difference between MsgBox and InputBox.

Visual Basic MsgBox( ) Function

When you as a programmer is developing software, you will consider some means of displaying messages to the program's users for a specific purpose either through label control or printing it on the form. However, sometimes you might need some actions to be taking based on the button click and using label and buttons to achieve this will be time consuming compare to message box.

What is the Purpose of MsgBox

The MsgBox function is used to produce a pop-up message box and prompt the user to click on a command button before any specified action can be executed.

Visual Basic MsgBox Syntax


MsgBox(Prompt, Style Value, Title)
In the syntax given above, the first argument, Prompt, will display the message in the message box. The Style Value will determine what type of command buttons appear on the message box, see

Message Command Buttons

for types of command buttons displayed on message box. The Title argument will display the title of the message board. Furthermore, there is still another message box function that is not supported in some Visual Basic versions e.g VB 6.0 and it has same Syntax as MsgBox

Visual Basic MessageBox.Show Syntax

MsgBox(Prompt, Style Value, Title)

Various Buttons that can be Added to a Message Box Buttons and Style Values

Style Value Name Constant Buttons Displayed
0vbOkOnlyOk button
1vbOkCancelOk and Cancel buttons
2vbAbortRetryIgnoreAbort, Retry and Ignore buttons
3vbYesNoCancelYes, No and Cancel buttons
4vbYesNoYes and No buttons
5vbRetryCancelRetry and Cancel buttons
We can use named constants in place of integers for the second argument to make the programs more readable. In fact, Visual Basic will automatically pop up list of named constants where you can select one of them. For example:

Dim msg As Integer
msg = MsgBox( "Are You Sure", 4, "Confirm")
and

Dim msg As Integer
msg = MsgBox("Are You Sure?", vbYesNo,"Confirm")
are the same.

Dim msg As Integer
msg = MessageBox.Show( "Are You Sure", 4, "Confirm")
and

Dim msg As Integer
msg = MessageBox.Show("Are You Sure?", vbYesNo,"Confirm")
are the same msg is a variable that holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the users. It has to be declared as Integer data type in the procedure or in the general declaration section.

Detecting Message Box Button Clicked


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim x As Integer
	x = MsgBox("Click any button and I will tell you its name", 1, "Test message")
	If x = 1 Then
		MsgBox("You have clicked the OK button")
	Else
		MsgBox("You have clicked the Cancel button")
	End If
End Sub
To make the message box looks advance, you can display an icon alongside the message. There are four types of icons available in Visual Basic as shown shown on the below table.

MsgBox Icons

Icons Value Name Constant
16 vbCritical
3 vbQuestion
48 vbExclamation
64 vbInformation
For instance, we can show question mark as instead of typing it. For example

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim msg As Integer
	msg = MsgBox("Do you want to quit the application", vbYesNoCancel + vbQuestion, "Confirm")
	If msg = 4 Then
		MsgBox("You have clicked the yes button")
	ElseIf msg = 7 Then
		MsgBox("You have clicked the No button")
	Else
		MsgBox("You have clicked the Cancel button")
	End If
End Sub
The first argument, Prompt, will display the message

Visual Basic InputBox( ) Function

If you want to accept inputs from users without using text boxes then input box serves the purpose. This will reduce the controls on your form as well as make your program light weight.

Purpose of an InputBox

An InputBox( ) function is used to display a message box alongside with input field where the user can enter a value or text based your the intention of the input box.

Visual Basic InputBox Syntax

For versions lower than 2012

VariableName = InputBox(Prompt, Title, default_text, x-position, y-position)
For 2012 versions The above syntax won't work because InputBox is considered a namespace. So, you need to key in the full reference to the Inputbox namespace as given below

VariableName = Microsoft.VisualBasic.InputBox(Prompt, Title, default_text, x-position, y-position)
VariableName is a variant data type but typically it is declared as string, which accept the message inputted by the users. The arguments are explained as follows:
  • Prompt: The message displayed normally as a question asked.
  • Title: The title of the Input Box.
  • default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to enter.
  • x-position and y-position - the position or tthe coordinates of the input box.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim userName As String
	userName = InputBox("Enter your name", "Name entry box", "", 60, 80)
	If userName <> "" Then
		MsgBox("Your name is: " & userName)
	Else
		MsgBox("No name inputted")
	End If
End Sub

Difference Between Message Box and Input Box

InputBox(): The InputBox() function is used to accept inputs from the user at run time MsgBox():Displays a dialog box containing a message, buttons, and optional icon to the user That's it. Thanks for reading

| | Views(3025)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services