Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more: (untagged)
how to use timer for time display in textbox i need c# code .
i am use visual studio 2010
Posted
Updated 11-Nov-12 2:25am
v2

Easy:
1) Add a timer to your form, either via the designer or via code.
2) Set the Interval property to 100 - this will cause 1/10th of a second updates.
3) Handle the Tick Event.
4) In the Tick handler, set the TextBox.Text property to DateTime.Now.ToString(formatString), where teh formatString defines what info you an't to display. Have a look here: Formatting a DateTime for display - format string description[^] for the format possibilities.

Doing the actual code is trivial, so I will leave it as an exercise for the reader! :laugh:
 
Share this answer
 
next time provide more details with your question.

C#
public Form1()
        {
            InitializeComponent();
        }
        private Timer tm = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
            tm.Tick += new EventHandler(tm_Tick);
            tm.Interval = 1000;
            tm.Enabled = true;
        }

        void tm_Tick(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToLongTimeString();
        }


select as answer, if it helps
 
Share this answer
 
v2
Comments
jahurul islam 11-Nov-12 23:26pm    
Thank you.
Member 11479008 18-Mar-15 1:51am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900