Home > DeveloperSection > Blogs > How to display current Time on C# form in Visual Studio 2012

How to display current Time on C# form in Visual Studio 2012


C# C# 
Ratings:
2 Comment(s)
 16464  View(s)
Rate this:

Step 1: Open Visual Studio 2012


  Step 2: Click on "New Project"


 


After clicking on "New Project" you will get screen like given below:-



Step 3: Select "Visual C#" from Project Type & "Windows Form Application" from Project Templates. 

Default name of new project will be "WindowsFormsApplication1". If You wish to rename Project Name, you can rename this from WindowsFomrsApplication1 to MyFirstProject OR DateTime OR as you wish. I renamed this as DateTime, After rename your Project Name Click on Ok Button as given is snap. After clicking on OK button you will get Blank form, you will see default name of form as "form1. 

Now you are ready to add controls on Blank Form.

Step 4: Select "Label" control from Toolbox (Just double click on "Lable" control or Drag & Drop it on form).

We will use here "Label" control to display Current Date & Time on C# Form.

Step 5: Select "Timer" control from Toolbox (Just double click on "Timer" control or Drag & Drop it on form).  

We will use here "Timer" control to enable & refresh Current Time on C# Form.
Now we are ready to code for current time using one "Label"e & one "Timer" Control.


Step 5: Double Click on form (OR Right Click on Form & Choose Option "view code").


You will get Code window as shown in snap. 


Now we have to add some little code for done our requirement as given below.

timer1.Enabled = true
timer1.Interval = 1000;

We are using first line of above code for active/enable "Timer" control in start-up of application & second line of code for refresh time after 1000 millisecond (1 second) respectively.

We have to write above both line of code after following line.

         InitializeComponent();

After adding two line of code, our code will look like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            timer1.Enabled = true;
            timer1.Interval = 1000;
        }

      
    }
}
    

Step 5: Right Click anywhere in code window & choose option "View designer" (Shift + F7).

Now you are return on your form. After that double click on timer control, you will be enter again in Code window. type following code under both {} (curly brackets). 

label1.Text = DateTime.Now.ToLongTimeString();


Code will look like below given line.


private void timer1_Tick(object sender, EventArgs e)
        {

            label1.Text = DateTime.Now.ToLongTimeString();

        }

Finally your full code will:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            timer1.Enabled = true; 
            timer1.Interval = 1000;
        }

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


Congrats! Your coding has been done now, Just press F5 to run your code or Go to Menu Bar > Debug > Start Debugging. You will get current time. THANK YOU. 





Current Time

By John Smith on   3 years ago
Good one.. Vilas..

Thank you

By Vilas Shende on   3 years ago
Thank you

Don't want to miss updates? Please click the below button!

Follow MindStick