Wyo VB Lecture Notes
Objective #1 - Use a MessageBox to
display output.
- In a MessageBox statement, you can use the Show method to display a message to the user in a modal dialog box. A dialog box is a small form with a message or question. A dialog box is modal, as opposed to modeless, if the user is forced to interact with it before returning to another form.
- A simple message box can be used as a standalone statement with only one string parameter to display a message and provides an OK button for the user to click to continue the program as in:
MessageBox.Show("Hello World")
- A second parameter can be specified to place a customized title in the title bar of the message box as in
MessageBox.Show("Hello World", "Fun Game")
- A third parameter can be specified to provide different sets of buttons. For example, the following example could be used to display a message box that contains a Cancel button in addition to the standard OK button for the user to click
answer = MessageBox.Show("Would you like to continue?", "Fun Game", MessageBoxButtons.OKCancel)
The value 1 will be returned by the Show method and stored in the variable answer of the user clicks the OK button. The value 2 will be stored in answer if the user clicks the Cancel button.
Instead of MessageBoxButtons.OKCancel, you can place the following other constants as the third parameter:
MessageBoxButtons.OK - provides a single OK button (this is the default)
MessageBoxButtons.OKCancel - provides OK and Cancel buttons
MessageBoxButtons.AbortRetryIgnore - provides three buttons, Abort, Retry, and Ignore
MessageBoxButtons.RetryCancel - provides Retry and Cancel buttons
MessageBoxButtons.YesNo - provides Yes and No buttons
MessageBoxButtons.YesNoCancel - provides three buttons, Yes, No, and Cancel
The following values are returned by the Show method in result of the user clicking the associated buttons:
1 OK
2 Cancel
3 Abort
4 Retry
5 Ignore
6 Yes
7 No
- A fourth parameter can be used with the Show method to include an icon on the message box. The following statement places an asterisk icon on the left side of the message box:
MessageBox.Show("Hello World", "Fun Game", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Other constants that can be used for different icons as the fourth parameter are
MessageBoxIcon.Asterisk, MessageBoxIcon.Error, MessageBoxIcon.Exclamation, MessageBoxIcon.Hand, MessageBoxIcon.Information, MessageBoxIcon.None, MessageBoxIcon.Question, MessageBoxIcon.Stop, and MessageBoxIcon.Warning
- If you don't want to specify a title caption but you do wish to specify a certain set of buttons, you must include the button constant as the third parameter and not the second parameter. You can place a blank space after the second parameter as in:
answer = MessageBox.Show("Would you like to continue?", " ", MessageBoxButtons.OKCancel)
- The full syntax for the MessageBox.Show method is
variable = MessageBox.Show(text [, caption][, buttons][, icon][, defaultButton][,
options])
where the square brackets are used to show optional parameters. If you desire, use the Visual Basic Help to learn about the defaultButton and options parameters.
- You can use a MessageBox to give the user a chance to confirm whether he/she really wants to exit your application. For example, in the form's FormClosing method, you can use the following:
Private Sub Form1_FormClosing(. . .)
Dim answer As Integer = 0 ' user's decision to exit or not
If (e.CloseReason = CloseReason.UserClosing) Then
answer = MessageBox.Show("Are you sure that you want to exit?", My.Application.Info.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (answer = 6) Then ' yes was clicked
Application.Exit()
Else
e.Cancel = True
End If
End If
End Sub
The value that is being compared to the variable answer is one of the button value's 1-7 listed above. You will not have to write out the If Else statement on any quiz or test in this VB class but you should understand it's role in the code segment above. We will not be studying e.Cancel but it is useful in situations like this.
Objective #2 - Use an InputBox to
obtain user input.
- Input boxes are similar to message boxes except they also provide a textbox for the user to fill in. An input box is appropriate to have the user input his or her name or a short answer to a question. Two buttons are included on an input box, OK and Cancel. If the user clicks OK, the data that he or she typed into the input box's text box will be returned and assigned to the variable in the assignment statement. For example:
gPlayerName = InputBox("What is your name?")
- Like an advanced MessageBox, an optional second parameter can be used to specify the text in the title bar at the top of the InputBox as in this example:
gPlayerName = InputBox("What is your name?", "Hello")
where the word "Hello" will appear in the title bar of the InputBox. But if you'd like the title bar to appear empty, then you can include a blank space (" ") as this second parameter:
gPlayerName = InputBox("What is your name?", " ")
- An optional third parameter can be provided to specify the default value that appears in the text box. This adds convenience to the user in case he doesn't want to type a personal answer:
gPlayerName = InputBox("What is your name?", " ", "Anonymous")
where the user is accepting the name "Anonymous" rather than providing his/her real name.
In a situation, where an age is being requested by an InputBox, the user could allow the programmer's default value of 0 rather than to provide his/her actual age as in this example:
age = InputBox("What is your age?", " ", "0")