Using a Variable for Output
You can display the contents of a variable by assigning the variable to a property (such as the Text property of a label object) or by passing the variable as an argument to a dialog box function. One useful dialog box function for displaying output is the MsgBox function. When you call the MsgBox function, it displays a dialog box, sometimes called a message box, with various options that you can specify. Like InputBox, it takes one or more arguments as input, and the results of the function call can be assigned to a variable. The syntax for the MsgBox function is
ButtonClicked = MsgBox(Prompt, Buttons, Title)
where Prompt is the text to be displayed in the message box; Buttons is a number that specifies the buttons, icons, and other options to display for the message box; and Title is the text displayed in the message box title bar. The variable ButtonClicked is assigned the result returned by the function, which indicates which button the user clicked in the dialog box.
If you're just displaying a message using the MsgBox function, the ButtonClicked variable, the assignment operator (=), the Buttons argument, and the Title argument are optional. You'll be using the Title argument, but you won't be using the others in the following exercise; for more information about them (including the different buttons you can include in MsgBox and a few more options), search for MsgBox Function in Visual Studio online Help.
NOTE
Visual Basic provides both the MsgBox function and the MessageBox class for displaying text in a message box. The MessageBox class is part of the System.Windows.Forms name-space, it takes arguments much like MsgBox, and it is displayed by using the Show method. I'll use both MsgBox and MessageBox in this book.
Now you'll add a MsgBox function to the Input Box program to display the name the user enters in the Input Box dialog box.
Display a message by using the MsgBox function
If the Code Editor isn't visible, double-click the Input Box button on the Input Box form.
The Button1_Click event procedure appears in the Code Editor. (This is the code you entered in the last exercise.)
Select the following statement in the event procedure (the last line):
Label1.Text = FullName
This is the statement that displays the contents of the FullName variable in the label.
Press the Delete key to delete the line.
The statement is removed from the Code Editor.
Type the following line into the event procedure as a replacement:
MsgBox(FullName, , "Input Results")
This new statement will call the MsgBox function, display the contents of the Full-Name variable in the dialog box, and place the words Input Results in the title bar. (The optional Buttons argument and the ButtonClicked variable are irrelevant here and have been omitted.) Your event procedure looks like this:
Click the Start Debugging button on the Standard toolbar.
Click the Input Box button, type your name in the input box, and then click OK.
Visual Basic stores the input in the program in the FullName variable and then displays it in a message box. Your screen looks similar to this:
Click OK to close the message box. Then click Quit to close the program.
The program closes, and the development environment returns.