Jun 20, 2012

Label in NET

Labels are one of the most frequently used C# control. We can use the Label control to display text in a set location on the page. Label controls can also be used to add descriptive text to a Form to provide the user with helpful information. But label control does not participate in user input or capture mouse or keyboard events. The Label class is defined in the System.Windows.Forms namespace. Here will discuss how to manage label in C#.NET.

A. Two ways of creating lables in .NET

1. Create label in design time

To create a Label control at design-time, you simply drag and drop a Label control from Toolbox to a Form. After you drag and drop a Label on a Form. After then you can move it around and resize it using mouse and set its properties and events.

2. Create label in run time

As we have said before, the Label class is defined in the System.Windows.Forms namespace and it represents a Label control.The following code snippet creates a Label control object.
// Create a Label object
Label myLabel = new Label();

B. Setting Label Properties
The easiest way to set properties is from the Properties Window. open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like:

Of course, We can set its properties by code in run time:
Example:
//set its name
myLabel.Name = "DynamicLabel";
//set label text
myLabel.Text = "I am Dynamic Label";
myLabel.TextAlign  = HorizontalAlignment.Center;
int size = myLabel.TextLength;








No comments:

Post a Comment