Using a Variable to Store Input


Using a Variable to Store Input

One practical use for a variable is to hold information from the user. Although you can often use an object such as a list box or a text box to gather this information, at times you might want to deal directly with the user and save the input in a variable rather than in a property. One way to gather input is to use the InputBox function to display a dialog box on the screen and then use a variable to store the text the user types. You'll try this approach in the following example.

Get input by using the InputBox function

  1. On the File menu, point to Open, and then click Project.

    The Open Project dialog box appears.

  2. Open the Input Box project in the c:\vb05sbs\chap05\input box folder.

    The Input Box project opens in the IDE. Input Box is a skeleton program.

  3. If the project's form isn't visible, click Form1.vb in Solution Explorer, and then click the View Designer button.

    The form contains one label and two buttons. You'll use the InputBox function to get input from the user, and then you'll display the input in the label on the form.

  4. Double-click the Input Box button.

    The Button1_Click event procedure appears in the Code Editor.

  5. Type the following program statements to declare two variables and call the InputBox function:

    Dim Prompt, FullName As String  Prompt = "Please enter your name."    FullName = InputBox(Prompt)  Label1.Text = FullName

    This time, you're declaring two variables by using the Dim statement: Prompt and FullName. Both variables are declared using the String type. (You can declare as many variables as you want on the same line, as long as they are of the same type.) Note that in Visual Basic 6, this same syntax would have produced different results. Dim would create the Prompt variable using the Variant type (because no type was specified) and the FullName variable using the String type. But this logical inconsistency has been fixed in Visual Basic versions 2002, 2003, and 2005.

    The second line in the event procedure assigns a text string to the Prompt variable. This message is used as a text argument for the InputBox function. (An argument is a value or an expression passed to a procedure or a function.) The next line calls the InputBox function and assigns the result of the call (the text string the user enters) to the Full-Name variable. InputBox is a special Visual Basic function that displays a dialog box on the screen and prompts the user for input. In addition to a prompt string, the InputBox function supports other arguments you might want to use occasionally. Consult the Visual Basic online Help for details.

    After InputBox has returned a text string to the program, the fourth statement in the procedure places the user's name in the Text property of the Label1 object, which displays it on the form.

    NOTE
    In older versions of BASIC, the InputBox function included a $ character at the end to help programmers remember that the function returned information in the string ($) data type. String variables were also identified with the $ symbol on occasion. These days we don't use character abbreviations for data types. String ($), Integer (%), and the other type abbreviations are now relics.

  6. Save your changes.

    Do you remember which toolbar button to click to save your project? See step 10 of the previous exercise if you've forgotten.

  7. Click the Start Debugging button on the Standard toolbar to run the program.

    The program runs in the IDE.

  8. Click the Input Box button.

    Visual Basic executes the Button1_Click event procedure, and the Input Box dialog box appears on your screen, as shown here:

    graphic

  9. Type your full name, and then click OK.

    The InputBox function returns your name to the program and places it in the FullName variable. The program then uses the variable to display your name on the form, as shown here:

    graphic

    Use the InputBox function in your programs anytime you want to prompt the user for information. You can use this function in combination with the other input controls to regulate the flow of data into and out of a program. In the next exercise, you'll learn how to use a similar function to display text in a dialog box.

  10. Click the Quit button on the form to stop the program.

    The program stops, and the development environment returns.

What Is a Function?

InputBox is a special Visual Basic keyword known as a function. A function is a statement that performs meaningful work (such as prompting the user for information or calculating an equation) and then returns a result to the program. The value returned by a function can be assigned to a variable, as it was in the Input Box program, or it can be assigned to a property or another statement or function. Visual Basic functions often use one or more arguments to define their activities. For example, the InputBox function you just executed used the Prompt variable to display dialog box instructions for the user. When a function uses more than one argument, commas separate the arguments, and the whole group of arguments is enclosed in parentheses. The following statement shows a function call that has two arguments:

FullName = InputBox(Prompt, Title)

Notice that I'm using italic in this syntax description to indicate that certain items are placeholders for information you specify. This is a style you'll find throughout the book and in the Visual Studio documentation.



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