8.1 Displaying Text with a Label Control

 <  Day Day Up  >  

8.1 Displaying Text with a Label Control

You want to use a Label on your Windows Form.


Technique

You use a Label control to display a string of text. Create a new Windows application and within the form designer, drag and drop a Label control from the toolbox onto the Windows Form. Next, type in the new text that you want displayed.

Comments

The Label control is also known as a static control because its only job is to display information to a user without any interaction. It primarily provides additional information regarding the function of a different control. For instance, a Windows Form that acts as an order form might have a Label control with the text "Address" followed by a TextBox control. The Label control in this instance provides immediate feedback to the user so there is no confusion about what to enter in that TextBox .

You can also programmatically create a Label control within your text. The previous chapter mentioned that Windows Forms technology isn't tied to a specified resource file format for user-interface definitions but rather uses dynamic code generation in real time as you define the user interface in the Windows Forms editor. Just as code is generated when defining the attributes of a Windows Form, the Integrated Development Environment (IDE) also generates the code necessary to associate a control with the Windows Form it is placed on. To view the code for a Label control, open the source code file associated with the Windows Form and expand the hidden source-code region named Windows Form Designer Generated Code. In the InitializeComponent method, you see the code necessary to create a label. In Listing 8.1, you can see the InitializeComponent method that is generated by the forms designer. A Label control is created and assigned to a private class member variable. After the label's properties are set, it is associated with and placed on the form by inserting the control into the form's Controls collection.

Listing 8.1 Code Generated by the Forms Designer for Controls on a Windows Form
 private System.Windows.Forms.Label label1; private void InitializeComponent() {     this.label1 = new System.Windows.Forms.Label();     this.SuspendLayout();     //     // label1     //     this.label1.Location = new System.Drawing.Point(88, 104);     this.label1.Name = "label1";     this.label1.Size = new System.Drawing.Size(120, 23);     this.label1.TabIndex = 0;     this.label1.Text = "This is a label control";     //     // Form1     //     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     this.ClientSize = new System.Drawing.Size(292, 266);     this.Controls.Add(this.label1);     this.Name = "Form1";     this.Text = "Form1";     this.ResumeLayout(false); } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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