Labels in C#


The Label control is probably the most frequently used control of them all.
Label is a simple control with one purpose only (to display text on the form).
Usually, you don’t need to add event handling code for a standard Label, although it does support events, like all controls.

You can set a number of properties for the Label control.

•AutoEllipsis: Gets or sets a value indicating whether the ellipsis character (...) appears at the right edge of the Label, denoting that the Label text extends beyond the specified length of the Label.



Design a Digital Clock


To design a simple digital clock we need the following controls and classes:

1 - labels: to display time on it.
2 - Timer Control: to call the time .

Enabled : Gets or sets a value indicating whether the Timer should raise the Elapsed event.
Interval: Gets or sets the interval at which to raise the Elapsed event.
Start: Starts raising the Elapsed event by setting
Enabled to true.
Stop: Stops raising the Elapsed event by setting
Enabled to false.

3 - DataTime class: to obtain time.

DateTime class is used to obtain date and time.
To return the current date and time we use
DateTime.Now
For example,

Datetime now = new DateTime.Now;



Now Lets start the design

First of all drag two labels to the form then drag a Timer Control and change the properties as following:




● Label1:
– Text: Time
– Font: 16 bold
● Label2:
– Text: empty
– Font: 16 bold
● Timer:
– Interval:1000

– Enable : true

Then write the following code in the form load

private void Form1_Load(object sender,
EventArgs e)
{
timer1.Start();
}

Then write the following code in the timer event

private void timer1_Tick(object sender,
EventArgs e)
{
DateTime now = DateTime.Now;
label2.Text = now.ToLongTimeString();
}




No comments:

Post a Comment