Visual Basic 2005 for Programmers (2nd Edition)

13.5. Labels, TextBoxes and Buttons

Labels 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.

Figure 13.17. Common Label properties.

Common Label properties

Description

Font

The font of the text on the Label.

Text

The text on the Label.

TextAlign

The alignment of the Label's text on the controlhorizontally (left, center or right) and vertically (top, middle or bottom).

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.

Figure 13.18. TextBox properties and an event.

TextBox properties and an event

Description

Common Properties

AcceptsReturn

If true in a multiline TextBox, pressing Enter in the TextBox creates a new line. If False, pressing Enter is the same as pressing the default Button on the Form. The default Button is the one assigned to a Form's AcceptButton property.

Multiline

If true, the TextBox can span multiple lines. The default value is False.

PasswordChar

When this property is set to a character, the TextBox becomes a password box, and the specified character masks each character the user types. If no character is specified, the TextBox displays the typed text.

ReadOnly

If TRue, the TextBox has a gray background, and its text cannot be edited. The default value is False.

ScrollBars

For multiline textboxes, this property indicates which scrollbars appear (None, Horizontal, Vertical or Both).

Text

The TextBox's text content.

Common Event

TextChanged

Generated when the text changes in a TextBox (i.e., when the user adds or deletes characters). When you double click the TextBox control in Design mode, an empty event handler for this event is generated.

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.

Figure 13.19. Button properties and an event.

Button properties and an event

Description

Common Properties

Text

Specifies the text displayed on the Button face.

FlatStyle

Modifies a Button's appearanceattribute Flat (for the Button to display without a three-dimensional appearance), Popup (for the Button to appear flat until the user moves the mouse pointer over the Button), Standard (three-dimensional) and System, where the Button's appearance is controlled by the operating system. The default value is Standard.

Common Event

Click

Generated when the user clicks the Button. When you double click a Button in design view, an empty event handler for this event is created.

Look-and-Feel Observation 13.3

Although Labels, TextBoxes and other controls can respond to mouse clicks, Buttons are more natural for this purpose.

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.

1 ' Fig. 13.20: FrmLabelTextBoxButtonTest.vb 2 ' Using a TextBox, Label and Button to display 3 ' the hidden text in a password TextBox. 4 Public Class FrmLabelTextBoxButtonTest 5 ' event handler for btnDisplayPassword's Click event 6 Private Sub btnDisplayPassword_Click(ByVal sender As System.Object, _ 7 ByVal e As System.EventArgs) Handles btnDisplayPassword.Click 8 ' display the text that the user typed 9 lblDisplayPassword.Text = txtInputPassword.Text 10 End Sub ' btnDisplayPassword_Click 11 End Class ' FrmLabelTextBoxButtonTest

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.

Категории