13.5. Labels, TextBoxes and ButtonsLabels provide text information (as well as optional images) and are defined with class Label (a derived class of Control). A Label displays text that the user cannot directly modify. A Label's text can be changed programmatically by modifying the Label's Text property. Figure 13.17 lists common Label properties.
A textbox (class TextBox) is an area in which text can be displayed by a program or the user can type text via the keyboard. A password TextBox is a TextBox that hides the information entered by the user. As the user types characters, the password TextBox masks the user input by displaying a character you specify (usually *). If you set the PasswordChar property, the TextBox becomes a password TextBox. Users often encounter both types of TextBoxes when logging into a computer or Web sitethe username TextBox allows users to input their usernames; the password TextBox allows users to enter their passwords. Figure 13.18 lists the common properties and a common event of TextBoxes.
A button is a control that the user clicks to trigger a specific action or to select an option in a program. As you will see, a program can use various button types, such as checkboxes and radio buttons. All the button classes derive from ButtonBase (namespace System.Windows.Forms), which defines common button features. In this section, we discuss class Button, which typically enables a user to issue a command to an application. Figure 13.19 lists common properties and a common event of class Button.
Look-and-Feel Observation 13.3
Figure 13.20 uses a TextBox, a Button and a Label. When the user clicks the Show Me Button, this application retrieves the text that the user typed in the password TextBox and displays it in a Label. Normally, we would not display this textthe purpose of password TextBoxes is to hide the text being entered by the user. When the user clicks the Show Me Button, this application retrieves the text that the user typed in the password TextBox and displays it in another TextBox. First, create the GUI by dragging the controls (a TextBox, a Button and a Label) onto the Form. Once the controls are positioned, change their names in the Properties window from the default valuestextBox1, button1 and label1to the more descriptive names lblDisplayPassword, btnDisplayPassword and txtInputPassword. The (Name) property in the Properties window enables us to change the variable name for a control. Visual Studio creates the necessary code and places it in method InitializeComponent of the partial class in the file FrmLabelTextBoxButtonTest.Designer.vb. Figure 13.20. Program to display hidden text in a password box.
We then set btnDisplayPassword's Text property to "Show Me" and clear the Text of lblDisplayPassword and txtInputPassword so that they are blank when the program begins executing. The BorderStyle property of lblDisplayPassword is set to Fixed3D, giving our Label a three-dimensional appearance. The BorderStyle property of all Text-Boxes is set to Fixed3D by default. The password character for txtInputPassword is set by assigning the asterisk character (*) to the PasswordChar property. This property accepts only one character. We create an event handler for btnDisplayPassword by double clicking this control in Design mode. We add line 9 to the event handler's body. When the user clicks the Show Me Button in the executing application, line 9 obtains the text entered by the user in txtInputPassword and displays the text in lblDisplayPassword. |