Displaying and Receiving Text: Using Labels and Text Boxes
One of the easiest ways to communicate information to and receive it from users is through text. You can display text about a program's functionality, and you can receive data as text from the user and use it in your program. Visual Basic provides two controls that are designed for displaying and receiving text. They are the Label and TextBox controls.
The Label control is the primary control for displaying text. It appears on the form as text enclosed in a rectangular-shaped area. The color of this area is usually the same as the color of the form. Therefore, it appears as if it is just text on the form.
Because a Label is primarily meant to display text, the most important properties for a Label control are the properties that control its appearance. The Text property contains the text that is displayed in the Label control. The Font property determines the display font for the text in the Text property. The ForeColor property determines the color of the text itself, and the BackColor property determines the color of the area surrounding the text.
When you have to both display and receive text, the TextBox control is designed to handle the job. In addition to being able to display text, the TextBox control enables users to type text into the TextBox at run time, and the program can retrieve that text.
As with the Label control, the properties that are most important for the TextBox control are those related to appearance. A significant property is the Text property, which represents the text in the TextBox control. When a user types in the TextBox control, the Text property is updated to reflect the changes. Thus, the text that shows in the TextBox control always reflects the value of the Text property.
There are also properties that affect the TextBox control's behavior. The Multiline property determines whether the TextBox control allows multiple lines. If this property is set to False, the TextBox control will always be exactly one line tall, and cannot be enlarged vertically. If set to True, the TextBox control allows multiple lines and can be as tall as you like.
To create a user interface with Label and Textbox controls
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type TextBoxExample, and then click OK.
A new Windows Forms project opens.
Enter your name and click the button.
Now that you have created a basic user interface, you will have to add a bit of code to your program, and then it will be ready to test!
To add code and test your program
Double-click the Button control to open the Code Editor.
The Code Editor opens the Button1_Click event handler.
Add the following line of code to the Button1_Click event handler.
Press F5 to run your program.
When the form appears, type your name in the TextBox control and click the button. A message box appears that displays the text in the TextBox control. Change the text and click the button again. Each time that you click the button, the updated text is displayed.
In this topic, you learned about the Label and TextBox controls, and how they can be used to display and receive text. In the next topic, you will learn how to create methods that handle control events. Although you've already learned how to create some basic event handlers, such as the Button_Click event handler, you will learn how to create methods to handle some of the other events that happen to controls.