This is the partial codes i wrote for an alarm clock application but why the counting down time don't worked? Thanks in advance :)

int intTimeDelay = 10;
        
        private void mnu5Sec_Click(object sender, EventArgs e)
        {
            intTimeDelay = 5;
            //intTimeDisplay = 5;
        }

        private void mnu10Sec_Click(object sender, EventArgs e)
        {
            intTimeDelay = 10;
            //intTimeDisplay = 10;
        }

        private void mnu15Sec_Click(object sender, EventArgs e)
        {
            intTimeDelay = 15;
            //intTimeDisplay = 15;
        }

        DateTime SnoozeTime;

        private void btnSNOOZE_Click(object sender, EventArgs e)
        {
            btnResetTime.Enabled = false;
            pnlDisplay.BackColor = SystemColors.Control;
            myPlayer.Stop();
            SnoozeTime = DateTime.Now.AddSeconds(intTimeDelay);
            TimeDelayed.Enabled = true;
            TimerCheck.Enabled = true;
        }

        private void TimeDelayed_Tick(object sender, EventArgs e)
        {
            int intTimeDisplay = intTimeDelay;

            for (int intCount = intTimeDelay; intCount >= 1; intCount--)
            {
                mnuTimeDisplay.Text = Convert.ToString(intTimeDisplay--) + " Secs";
            }
        }

The code in your Tick event runs every time the timer's interval has elapsed. SO if your interval is set to 1 second (ie 1000 ms) then once every second you are displaying a countdown from intTimeDelay to 1.
You need to store the remaining time in a variable outside the tick event (or use the intTimeDelay if you don't need to maintain its value after the countdown). Then each time the timer ticks, you reduce the remaining time by 1 second and display it.

When working with the timer, remember that the code in the Tick event runs every time the timer ticks, so putting a loop inside the tick event means the loop will run every tick. If you want one step of the loop to run each time the tick happens you need to put the counter outside the tick event and increment it by one step inside the tick event:

int Counter;

private void Button_Click(object sender, EventArgs e)
{
    Counter = 0;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    if(Counter < 10)
    {
        label1.Text = Counter.ToString();
    }
    else
    {
        timer1.Stop()
    }
    Counter++;   
}

Edited 6 Years Ago by Geekitygeek: n/a

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