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

TextBox properties and events

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 type. 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 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 application. Figure 13.19 lists common properties and a common event of class Button.

Figure 13.19. Button properties and event.

Button properties and events

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

Figure 13.20. Program to display hidden text in a password box.

 1 // Fig. 13.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 // Form that creates a password TextBox and
 8 // a Label to display TextBox contents
 9 public partial class LabelTextBoxButtonTestForm : Form
10 {
11 // default constructor
12 public LabelTextBoxButtonTestForm()
13 {
14 InitializeComponent();
15 } // end constructor
16
17 // display user input in Label 
18 private void displayPasswordButton_Click( 
19  object sender, EventArgs e ) 
20 { 
21  // display the text that the user typed 
22  displayPasswordLabel.Text = inputPasswordTextBox.Text;
23 } // end method displayPasswordButton_Click 
24 } // end class LabelTextBoxButtonTestForm
 

First, create the GUI by dragging the controls (a TextBox, a Button and a Label) on the Form. Once the controls are positioned, change their names in the Properties window from the default valuestextBox1, button1 and label1to 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 then set displayPasswordButton's Text property to "Show Me" and clear the Text of displayPasswordLabel and inputPasswordTextBox so that they are blank when the program begins executing. The BorderStyle property of displayPasswordLabel is set to Fixed3D, giving our Label a three-dimensional appearance. The BorderStyle property of all TextBoxes is set to Fixed3D by default. The password character for inputPasswordTextBox is set by assigning the asterisk character (*) to the PasswordChar property. This property accepts only one character.

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

GroupBoxes and Panels

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net