Chapter 3 Working with Toolbox Controls 71
As you learned in Chapter 2, “Writing Your First Program,” buttons are used to get
the most basic input from a user. When a user clicks a button, he or she is requesting
that the program perform a specific action immediately. In Visual Basic terms, the
user is using the button to create an event that needs to be processed in the program.
Typical buttons in a program are the OK button, which a user clicks to accept a list of
options and to indicate that he or she is ready to proceed; the Cancel button, which
a user clicks to discard a list of options; and the Quit button, which a user clicks to exit
the program. In each case, you should use these buttons in the standard way so that
they work as expected when the user clicks them. A button’s characteristics (like those
of all objects) can be modified with property settings and references to the object
in program code.
- Set the following property for the button object by using the Properties window:
Object Property Setting
Button1 Text ”OK”
For more information about setting properties and reading them in tables, see the
section entitled “The Properties Window” in Chapter 1.
- Double-click the OK button, and type the following program statement
between the Private Sub Button1_Click and End Sub statements in the
Code Editor:
TextBox1.Text = "Hello, world!"
Note As you type statements, Visual Studio displays a list box containing all valid items
that match your text. After you type the TextBox1 object name and a period, Visual Studio
displays a list box containing all the valid properties and methods for text box objects,
to jog your memory if you’ve forgotten the complete list. This list box is called Microsoft
IntelliSense and can be very helpful when you are writing code. If you click an item in the
list box, you will typically get a tooltip that provides a short description of the selected
item. You can add the property from the list to your code by double-clicking it or by using
the arrow keys to select it and then pressing TAB. You can also continue typing to enter
the property yourself. (I usually just keep typing, unless I’m exploring new features .)
The statement you’ve entered changes the Text property of the text box to “Hello,
world!” when the user clicks the button at run time. (The equal sign (=) assigns
everything between the quotation marks to the Text property of the TextBox1 object .)
This example changes a property at run time—one of the most common uses of
program code in a Visual Basic program.
Now you’re ready to run the Hello program.