Hi

I am trying to display time in a text box which updates itself every second. I am using visual C# and I am pretty new to it. (My first program in visual C#)
(Actually its part of a little bigger problem in which I have to implement a timer class, which will show me how much time has been elapsed since I have started the program. Again this elapsed time will update it every second)


I know this can be done using timer class but I don't know exactly how to use it.
I found a similar thread at DaniWeb, but I am stuck at "how to display it" and where are all the ticks thing that they talk about. Also I tried to use object browser but seems like I am unable to interpret what each thing displayed in object browser does.


So here is my code:

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

namespace Timer
{
    public partial class Timer : Form
    {
        public Timer()
        {
            InitializeComponent();
        }
     
     
        private void Form1_Load(object sender, EventArgs e)
        {
            String hours;
            String minutes;
            String seconds;

            hours = DateTime.Now.Hour.ToString();
            minutes = DateTime.Now.Minute.ToString();
            seconds = DateTime.Now.Second.ToString();
            textBox1.Text = "System Time - " + hours + ":" + minutes + ":" + seconds;
            timer1.Enabled = false;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToString("hh:mm:ss tt");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

    }
}

I have shown the time using datetime.now, but how to update it everysecond.
Other way I see is timer, setting interval to 1000, and showing it in the textbox. how to get the time of timer into the text box.

Design of the form attached as a reference.

Hi guys...
I actually got this already working. I just did not know how TimerObject_tick() function works. Now I know it constantly update itself (which is still funny and a little vague for me) after the interval we set.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.