heyz... i need help. i new to C#. i need to add timer to my code. but i don't know how to do it... can help mi!! all i have done to the code is declared int time. :(:(

oh no.... this is not what i wanted... i jus want to show the time and date in my GUI. how to do that??

Here is an example of how to use a timer. Important to know: Timer tick is the method which is called when the timer come to zero:

public partial class Form1 : Form
    {
        Timer timer1;
        int counter;

        public Form1()
        {
            InitializeComponent();
            label1.Text = "countier: 0";
            CreatingTimer();
        }

        private void CreatingTimer()
        {
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            timer1.Enabled = true;
            label1.Text = "counter: " + counter.ToString();
        }

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

Welcome Baby G

Drop a Label onto the Form and write following code in Form Load event

private void Form1_Load(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 1000; //1sec
            timer.Tick+=(sa,ea)=>
                {
                    label1.Text = DateTime.Now.ToString();
                };
            timer.Start();
        }

Edited 5 Years Ago by __avd: n/a

does this codes work for visual studios ?? i tried but the output is still the same... as in no change at all!!

does this codes work for visual studios ??

Yes.

i tried but the output is still the same... as in no change at all!!

Hey, come on! ;) Please show us your code work.

hehe....sorry i made a mistake... actually i can work ^^ thank you!!

This question has already been answered. Start a new discussion instead.