14.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 14.17 lists common Label properties.

Fig. 14.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 control—horizontally (left, center or right) and vertically (top, middle or bottom). The default is top, left.

A textbox (class TextBox) is an area in which either 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 password character. If you set the property UseSystemPasswordChar to true, the TextBox becomes a password TextBox. Users often encounter both types of TextBoxes, when logging into a computer or website—the username TextBox allows users to input their usernames; the password TextBox allows users to enter their passwords. Figure 14.18 lists the common properties and a common event of TextBoxes.

Fig. 14.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 (the default), 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.
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—the default, Horizontal, Vertical or Both).
Text The TextBox’s text content.
UseSystemPasswordChar When true, the TextBox becomes a password TextBox, and the system-specified character masks each character the user types.
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’ll see, a program can use several types of buttons, such as checkboxes and radio buttons. All the button classes derive from class 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 app. Figure 14.19 lists common properties and a common event of class Button.

Fig. 14.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 appearance—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.

Figure 14.20 uses a TextBox, a Button and a Label. The user enters text into a password box and clicks the Button, causing the text input to be displayed in the Label. Normally, we would not display this text—the purpose of password TextBoxes is to hide the text being entered by the user. When the user clicks the Show Me Button, this app retrieves the text that the user typed in the password TextBox and displays it in a Label.

Fig. 14.20 Program to display hidden text in a password box.

Alternate View

  1    // Fig. 14.20: LabelTextBoxButtonTestForm.cs
  2    // Using a TextBox, Label and Button to display
  3    // the hidden text in a password TextBox.
  4    using System;
  5    using System.Windows.Forms;
  6
  7    namespace LabelTextBoxButtonTest
  8    {
  9       // Form that creates a password TextBox and
 10       // a Label to display TextBox contents
 11       public partial class LabelTextBoxButtonTestForm : Form
 12       {
 13          // default constructor
 14          public LabelTextBoxButtonTestForm()
 15          {
 16             InitializeComponent();
 17          }
 18
 19          // display user input in Label
 20          private void displayPasswordButton_Click(object sender, EventArgs e)
 21          {
 22              // display the text that the user typed
 23              displayPasswordLabel.Text = inputPasswordTextBox.Text;
 24          }
 25       }
 26    }

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 values—textBox1, button1 and label1—to the more descriptive displayPasswordLabel, displayPasswordButton and inputPasswordTextBox. 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 LabelTextBoxButtonTestForm.Designer.cs.

We set displayPasswordButton’s Text property to “Show Me” and clear the Text of displayPasswordLabel so that it’s blank when the program begins executing. The BorderStyle property of displayPasswordLabel is set to Fixed3D, giving our Label a three-dimensional appearance. We also changed its TextAlign property to MiddleLeft so that the Label’s text is displayed centered between its top and bottom. The password character for inputPasswordTextBox is determined by the user’s system settings when you set UseSystemPasswordChar to true.

We create an event handler for displayPasswordButton by double clicking this control in Design mode. We added line 23 to the event handler’s body. When the user clicks the Show Me Button in the executing app, line 23 obtains the text entered by the user in inputPasswordTextBox and displays the text in displayPasswordLabel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset