Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.
<<PreviousNext>>

Text Property of the Text Box

Similar to the label and the button, the text box has a property called Text. At design time, you can set this property to any text, which will then be displayed at runtime, much like the label you saw in Chapter 2. Unlike the label, however, the text box also allows the user to enter and edit its content at runtime. The text can then be further processed, or simply saved for future use.
As an exercise, place a text box in a new form by double-clicking the TextBox icon in the Toolbox. You will see a blank box in the form. Adjust its size by dragging one of its sizing handles; and change its location as you see fit by dragging the control from inside the boundary. Because you are interested in using the text box for inputting your friend’s name, you should name the control txtName. You can change the control’s name by typing txtName in the Name property in the Properties window.
Look at the form again and you will realize that a blank box in a form means little to the user because it provides no clue as to what the user is supposed to do with it.  You can place a label with proper text such as Name (Last, First Init.) by the text box. Such a clue is indispensable. Text boxes are almost always accompanied with labels when used for input purposes. Your result should look similar to that shown in Figure 3-1.

Figure 3-1
Making the label and text box work together

After you have the label and the text box in the form, you can run the program and enter your name into the text box. To see what the computer can do with what you have entered, place the following code in the form’s click event:

Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click MsgBox("Your name is " & txtName.Text)

Recall that to create a code template for an event procedure, you need to use the object box and procedure box in the code window. Refer to Figure 2-12 for specifics.

Tip
Both the label and text box controls can be used to display text. So, how do you decide which one to use? If you don’t want the user to change its content, use the label. If you need the user’s input or if you want to allow the user to copy and paste from the box, use the text box. You can make a label look similar to a text box by setting its BackColor property to ActiveCaptionText (White) and its BorderStyle property to Fixed3D.
Run the program again. Enter your name and then click the form. You should see a message box displaying the message, “Your name is” along with your name. Close the program before proceeding.
In the preceding code, the & operator concatenates the text string ”Your name is ”  with the content of the text box named txtName. If you enter “Smith, John” as your name in the text box, the resulting text string will be ”Your name is Smith, John.” Figure 3-2 shows the result. String operations are discussed in more detail in Chapter 4, “Data, Operations, and Built-In Functions.” The MsgBox displays the text in a dialog box and waits until the user acknowledges the message by clicking the OK button or presses the space bar on the keyboard. The MsgBox is discussed further in Chapter 6, “Input, Output, and Procedures.”

Figure 3-2
Showing what’s entered in the text box


You can create the phone number field on the form in the same manner:

The user interface is now ready for another test. Start the program, and try to enter a friend’s name and phone number. A sample result is shown in Figure 3-3.
Figure 3-3
Text boxes in action

Understanding the Focus

Because you have more than one control to accept input, you can enter data into a control only when it has the focus. A user can set the focus of a control by clicking on it or by pressing the Tab key repetitively until the focus reaches that control. When the text box gets the focus, a blinking cursor will appear in its box.
Your program can also set the focus on a particular control by using the control’s Focus method. For example, the following statement will set the focus on txtName.

txtName.Focus

This method can be useful when you find a need to set the focus on some control after certain operations. For example, assume you have added a button named btnSave to the form, and imagine the user has just clicked the Save button, triggering the Click event procedure to save data on screen. After the data are saved, however, the focus remains on the button. By including the statement, you can make it convenient for your user to continue the data-entry operation. The code structure should appear as follows:

Private Sub btnSave_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click ' Statements to save data MsgBox("Data saved") 'Tell the user data have been saved ' Reset focus on the first control txtName.Focus() End Sub

Last change: February 13 2016 18:47:28.
<<PreviousNext>>