Using a Variable for Output


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

  1. 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.)

  2. 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.

  3. Press the Delete key to delete the line.

    The statement is removed from the Code Editor.

  4. 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:

    graphic

  5. Click the Start Debugging button on the Standard toolbar.

  6. 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:

    graphic

  7. Click OK to close the message box. Then click Quit to close the program.

    The program closes, and the development environment returns.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net