Вы находитесь на странице: 1из 3

Lesson VI.

MsgBox() and InputBox()

Objectives

To be able to use the MsgBox Function


To be able to use the InputBox Function

Notes
You use Message and Input Boxes when you need to ask the user questions or display error
messages and advise the user.

Input Box

Message Box

Input Box

IN FOCUS: MESSAGE BOX


The format of the Message Box function is as follows:
<intVariable> = MsgBox(<str_message>, <int_type>, <str_title>)
<intVariable> is an Integer variable that receives the return value of the function. If you do not
need to determine which button the user clicked (for example, when there is only one button), you
may use the function by executing:
MsgBox<str_message>, <int_type>, <str_title>
<str_message> is a String literal or a String variable that contains the message to be displayed in
the Message Box. <int_type> is an optional numeric value or expression the describes the options
you want in the Message Box. This value is the sum of the values of the options (see the tables
below) you want to invoke. For example, if you want to display the Yes and No buttons, vbQuestion
icon, and set the No button as the default button, then the value of <int_type> is 292 (4 + 32 +
256). You may also use their corresponding named literal as in vbYesNo + vbQuestion +
vbDefaultButton2. <str_title> is a String literal or variable that contains the text that appears in the
Title Bar of the Message Box.
Named Literal
vbOkOnly
vbOkCancel
vbAbortRetryIgnore

Valu
e
0
1
2

vbYesNoCancel

vbYesNo
vbRetryCancel

4
5

Description
Displays
Displays
Displays
buttons
Displays
buttons.
Displays
Displays

the OK button.
the OK and Cancel buttons.
the Abort, Retry, and Ignore
the Yes, No, and Cancel
the Yes and No buttons.
the Retry and Cancel buttons.

The Buttons displayed in a Message Box

Named Literal
vbCritical
vbQuestion
VbExclamation
vbInformation

Valu
e
16
32
48
64

Description
Displays
Displays
Displays
Displays

the Critical Message Icon.


the Warning Query Icon.
Warning Message Icon.
Information Message Icon.

The Icons displayed in a Message Box

Named Literal
vbDefaultButton1
vbDefaultButton2

Valu
e
0
256

Description
The first button is the default.
The second button is the default.

VbDefaultButton3

512

The third button is the default.

The Default Buttons displayed in a Message Box

If your Message Box has more than 1 button, you determine which button is pressed by checking
the value of <intVariable>. The return values are enumerated in the following table.
Named Constant
vbOk
vbCancel
vbAbort
vbRetry
vbIgnore
vbYes
vbNo

Valu
e
1
2
3
4
5
6
7

Description
The
The
The
The
The
The
The

user
user
user
user
user
user
user

clicked
clicked
clicked
clicked
clicked
clicked
clicked

the
the
the
the
the
the
the

Ok button.
Cancel button.
Abort button.
Retry button.
Ignore button.
Yes button.
No button.

MsgBox()s Return Values

The sample Message Box on the previous page was created through:
MsgBox "You must enter A, B, or C.", vbCritical, "Error"
IN FOCUS: INPUT BOX
Input Box is very similar to a Message Box but instead of the 7 return values that indicate which
button was pressed, the Input Box returns a string data value entered by the user.
The following is the format of Input Box function:
<strVariable> = InputBox (<str_prompt>, <str_title>, <str_default>, <intXPos>, <intYPos>)
<str_prompt> works like <str_message> value in Message Box. It is the text that appears in the
Input Box. It is usually an instruction on what the user should enter in the TextBox provided for in
the Input Box. <str_title> is what appears in the Title Bar. <str_default> is the default String in the
TextBox. <intXPos> and <intYpos> determine the position of the Input Box relative to the Form.
They hold the number of twips from the top and left edge of the Form window to the top and left
edge of the Input Box, respectively. <str_title>, <str_default>, <intXPos>, <intYpos> are optional
parameters.
Input Boxes always containt Ok and Cancel buttons. If the user presses the Ok button, the function
returns the String entered in the TextBox. If the user presses the Cancel button, a null String () is
returned.
Examples:
MsgBox("This is a MessageBox", vbRetryCancel + vbInformation, "Sample")

MsgBox("This is another MessageBox", vbAbortRetryIgnore + vbDefaultButton2 + vbExclamation,


"Sample 2")

InputBox("This is an Input Box", "Sample", "test")

Lesson in Action
The following application uses both Input and Message Boxes. When the application is executed
(therefore, place your code in the Form_Load procedure), an Input Box will ask for a number
from 1 to 10. A Message Box is displayed when the user enters a number not in the range.
When a valid entry is received, simply display that value in a Label on the Form.
The following are screen shots of the Input and Message Boxes.

Write the following procedure:


Private Sub Form_Load()
num = InputBox("Enter a number from 1 to 10.", "Input", "")
If ((num < 1) Or (num > 10)) Then
MsgBox "Please enter a number from 1 to 10 only!", vbCritical, "Invalid Entry"
Else
lblOutput.Caption = "You entered number " & num & "."
End If
End Sub
Save your work as Lesson6.vbp.

On your Own
1.

Write an application that asks for your age through an InputBox() and then displays your
age 5 years ago through a MsgBox(). Display a default value of 10 in the InputBox. Note:
if the age entered is <=5, output You were not yet born 5 years ago. The InputBox
should automatically appear during program startup.

2.

Create a program with one CommandButton and one Label. When the user presses the
button, a MsgBox appears with 3 buttons: Yes, No, Cancel. Display which button was
clicked in the Label.

Вам также может понравиться