Timer control in c# windows application

We will make one example for understanding timer control in c# windows application.

Here, we display current Date and time on windows form using timer windows control.

First we design windows form using Label control for Display current Date and time using Timer control in asp.net c#.

Timer control in c# windows application
Timer control in c# windows application

In above figure we took a timer control from toolbox on windows form in asp.net

Now tack label control on windows form for display current time and date.

Below figure we can see the static time on label control in windows application….

Display current Date and Time in windows application
Display current Date and Time in windows application

write below code on page load event of windows form.

lbltime.Text = System.DateTime.Now.ToString();
lblday.Text = System.DateTime.Now.DayOfWeek.ToString();

If we want to display running time on label control so we need to use timer control timer_tick event..

Timer_Tick event fire according to the properties of Timer Interval of timer control.

Timer control in windows application
Timer control in windows application

what is ajax in asp.net?

How to use ajax timer control in asp.net

Now we can write below code on Time_tick Events

private void timer1_Tick(object sender, EventArgs e)
{
lbltime.Text = System.DateTime.Now.AddSeconds(1).ToString();
}

and write below code on page load event for display date and time on windows form.

private void Form1_Load(object sender, EventArgs e)
{
lbltime.Text = System.DateTime.Now.ToString();
lblday.Text = System.DateTime.Now.DayOfWeek.ToString();

lbldate1.Text = System.DateTime.Now.GetDateTimeFormats()[2].ToString();
lbldate2.Text = System.DateTime.Now.GetDateTimeFormats()[3].ToString();
lbldate3.Text = System.DateTime.Now.GetDateTimeFormats()[5].ToString();
lbldate4.Text = System.DateTime.Now.GetDateTimeFormats()[8].ToString();
lbldate5.Text = System.DateTime.Now.GetDateTimeFormats()[10].ToString();
}

The out put of time control in c# windows application is:

date and time format in windows application c#
date and time format in windows application c#

In above figure we can see the different date and time format in windows application c#.

I hope this timer control example for displaying current date and time will help you……..

Leave a Reply

Your email address will not be published. Required fields are marked *