Label s, TextBox es and Button s

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 574 - 577)

2. When the app executes, another compiler (known as the just-in-time compiler

14.5 Label s, TextBox es and Button s

Using Visual Studio To Edit a GUI’s Layout

Visual Studio helps you with GUI layout. When you drag a control across aForm, bluesnap linesappear to help you position the control with respect to others (Fig. 14.16) and the

Form’s edges. This feature makes the control you’re dragging appear to “snap into place”

alongside other controls. Visual Studio also provides theFORMATmenu, which contains options for modifying your GUI’s layout. TheFORMATmenu does not appear in the IDE unless you select one or more controls in design view. When you select multiple controls, you can align them with theFORMATmenu’sAlignsubmenu. TheFORMATmenu also en- ables you to modify the space between controls or to center a control on theForm.

14.5 Label s, TextBox es and Button s

Labels provide text information (as well as optional images) and are defined with classLa-

bel(a derived class ofControl). ALabeldisplays text that theuser cannot directly modify.

ALabel’s text can be changed programmatically by modifying theLabel’sTextproperty.

Figure 14.17 lists commonLabelproperties.

Atextbox(classTextBox) is an area in which either text can be displayed by a program or the user can type text via the keyboard. ApasswordTextBoxis aTextBoxthat hides the information entered by the user. As the user types characters, the passwordTextBoxmasks the user input by displaying a password character. If you set the propertyUseSystemPass- wordChartotrue, theTextBoxbecomes a passwordTextBox. Users often encounter both types ofTextBoxes, when logging into a computer or website—the usernameTextBox allows users to input their usernames; the passwordTextBox allows users to enter their passwords. Figure 14.18 lists the common properties and a common event ofTextBoxes.

Fig. 14.16 | Snap lines for aligning controls.

CommonLabel

properties Description

Font The font of the text on theLabel.

Text The text on theLabel.

TextAlign The alignment of theLabel’s text on the control—horizon- tally (left, center or right) and vertically (top, middle or bot- tom). The default is top, left.

Fig. 14.17 | CommonLabelproperties.

Snap line to help align controls on their left sides

Snap line that indicates when a control reaches the minimum recommended distance from another control or the edge of aForm

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 (namespaceSystem.Windows.Forms), which defines common button features. In this sec- tion, we discuss classButton, which typically enables a user to issue a command to an app.

Figure 14.19 lists common properties and a common event of classButton.

TextBoxproperties

and an event Description

Common Properties

AcceptsReturn Iftruein a multilineTextBox, pressingEnterin theTextBoxcreates a new line. Iffalse(the default), pressingEnteris the same as pressing the defaultButtonon theForm. The defaultButtonis the one assigned to aForm’sAcceptButtonproperty.

Multiline Iftrue, theTextBoxcan span multiple lines. The default value isfalse.

ReadOnly Iftrue, theTextBoxhas a gray background, and its text cannot be edited. The default value isfalse.

ScrollBars For multiline textboxes, this property indicates which scrollbars appear (None—the default,Horizontal,VerticalorBoth).

Text TheTextBox’s text content.

UseSystem- PasswordChar

Whentrue, theTextBoxbecomes a passwordTextBox, and the system- specified character masks each character the user types.

Common Event

TextChanged Generated when the text changes in aTextBox(i.e., when the user adds or deletes characters). When you double click theTextBoxcontrol in Designmode, an empty event handler for this event is generated.

Fig. 14.18 | TextBoxproperties and an event.

Buttonproperties

and an event Description

Common Properties

Text Specifies the text displayed on theButtonface.

FlatStyle Modifies aButton’s appearance—Flat(for theButtonto display without a three-dimensional appearance),Popup(for theButtonto appear flat until the user moves the mouse pointer over theButton),Standard (three-dimensional) andSystem, where theButton’s appearance is con- trolled by the operating system. The default value isStandard. Common Event

Click Generated when the user clicks theButton. When you double click a

Buttonin design view, an empty event handler for this event is created.

Fig. 14.19 | Buttonproperties and an event.

14.5 Labels,TextBoxes andButtons 535

Figure 14.20 uses aTextBox, aButtonand aLabel. The user enters text into a pass- word box and clicks theButton, causing the text input to be displayed in theLabel. Nor- mally, we would not display this text—the purpose of passwordTextBoxes is to hide the text being entered by the user. When the user clicks theShow MeButton, this app retrieves the text that the user typed in the passwordTextBoxand displays it in aLabel.

First, create the GUI by dragging the controls (aTextBox, aButtonand aLabel) onto theForm. Once the controls are positioned, change their names in thePropertieswindow from the default values—textBox1,button1andlabel1—to the more descriptivedis-

playPasswordLabel,displayPasswordButtonandinputPasswordTextBox. The(Name) property in thePropertieswindow enables us to change the variable name for a control.

Visual Studio creates the necessary code and places it in methodInitializeComponentof the partial class in the fileLabelTextBoxButtonTestForm.Designer.cs.

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 } // end constructor 18

19 20 21 22 23 24 25

26 } // end class LabelTextBoxButtonTestForm 27 } // end namespace LabelTextBoxButtonTest

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

// display user input in Label

private void displayPasswordButton_Click(

object sender, EventArgs e ) {

// display the text that the user typed

displayPasswordLabel.Text = inputPasswordTextBox.Text;

} // end method displayPasswordButton_Click

We setdisplayPasswordButton’sTextproperty to “Show Me” and clear theTextof

displayPasswordLabelso that it’s blank when the program begins executing. TheBor-

derStyleproperty ofdisplayPasswordLabelis set toFixed3D, giving ourLabela three- dimensional appearance. We also changed itsTextAlignproperty toMiddleLeftso that theLabel’s text is displayed centered between its top and bottom. The password character forinputPasswordTextBoxis determined by the user’s system settings when you setUse-

SystemPasswordChartotrue.

We create an event handler fordisplayPasswordButtonby double clicking this con- trol inDesignmode. We added line 24 to the event handler’s body. When the user clicks theShow MeButtonin the executing app, line 24 obtains the text entered by the user in

inputPasswordTextBoxand displays the text indisplayPasswordLabel.

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 574 - 577)

Tải bản đầy đủ (PDF)

(1.020 trang)