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 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
intAnswer = 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 intAnswer of the user clicks the OK button. The value 2 will be stored in intAnswer 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 in the lower-left corner 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:
intAnswer = 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 (instead of FormClosed
method), you can use the following:
Private Sub Form1_Closing(. . .)
Dim intAnswer As Integer = 0
intAnswer = MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo)
If (intAnswer = 7) Then
e.Cancel = True
Else
Application.Exit()
End If
End Sub
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 can accept a maximum of 255 characters. 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. Unlike a message box statement, you would never use an InputBox statement as a standalone statement. Here is an example
strName = InputBox("Enter your name:")
- Like the message box that is explained above, an optional second parameter can be used to specify the title of the input box's modal dialog box.
- A third parameter can be provided to specify the default value that appears in the text box.
strUserInput = InputBox("Enter your age:", " ", "0")
Notice that no title was provided in the example above but the second comma had to be included.
- The value entered by the user and returned by the InputBox method may be treated by VB as a string. It is safest to convert this value to a numeric value using the Val function or another means as in
Dim strUserInput As String
Dim intAge As Integer = 0
strUserInput = InputBox("Enter your age:")
intAge = Val(strUserInput)