Skip to content

Timer control, how to use it for a countdown.

Christian Parent

Asked Jun 8, 2007, 2:02 PM EDT11 answers
Share
LinkedInX
I am working on an exercise in the book i am using to learn programming and C#, its a math game, i have added a timer so it would let 30 seconds to answer. What i would like to do, is to show that timer (the countdown) is a text box.

I dont know if it is the easiest or best way but, what i though, was to add a while loop, what I set to 30 and dercrmenting at every ticks (setting the timer at 1 sec). Here is my code without that feature. ( I know i could had use Switch for some of the if but, i am not there yet in the book. The countdown is not part of the book, it's just an idea i got)

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Math_Game
{
public partial class Form1 : Form

{
Random randomNo = new Random();
int no1, no2;
string problemType = "+";
public Form1()
{
InitializeComponent();
}
private void StartGame_Click(object sender, EventArgs e)
{
timerReset();
StartGame.Visible = false;
CheckAnswer.Visible = true;
Question.Visible = true;
Answer.Visible = true;
NextQuestion.Visible = true;
Feedback.Visible = true;
Gen_Question();
}
private void Gen_Question()
{
no1 = randomNo.Next(100);
no2 = randomNo.Next(100);

if (radioButton1.Checked)
{
problemType = "+";
}
else if (radioButton2.Checked)
{
problemType = "-";
}
else if (radioButton3.Checked)
{
problemType = "*";
}
Question.Text = no1 + problemType + no2 + "=";
}
private void CheckAnswer_Click(object sender, EventArgs e)
{
timer1.Stop;
int result = 0;
if (problemType == "+")
{
result = no1 + no2;
}
else if (problemType == "-")
{
result = no1 - no2;
}
else if (problemType == "*")
{
result = no1 * no2;
}
if (Answer.Text == result.ToString())
{
Feedback.Text = "Correct";
}
else
{
Feedback.Text = "Incorrect";
}
}
private void NextQuestion_Click(object sender, EventArgs e)
{
Gen_Question();
timerReset();
Answer.Visible = true;
Answer.Text = "";
Feedback.Text = "";
}
private void timer1_Tick(object sender, EventArgs e)
{
Answer.Visible = false;
Feedback.Text = "Trop tard";
timer1.Stop;
}
private void timerReset()
{
timer1.Interval = 30000;
timer1.Start();
}
}
}

11 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    You definitely DON'T want to do it in a while loop.  You would hang up the program and the gui.  The way to handle a count down is to change your timer event handler and the interval;
    // change to 1 second interval
    timer.Interval = 1000;

    private void timer1_Tick(object sender, EventArgs e)
    {
      textBox.Text =  _count.ToString(); 

      _count--;
      if (_count == 0)
       {
          _count = 30;
         Answer.Visible = false;
         Feedback.Text = "Trop tard";
     
          timer.Stop();
       }
    }
    0
  • 0

    Try this out:

    /*Put this variable at the top of your class right after the namespace declaration
    I put the variable "i" here because if you put it in the timer1_tick void, it
    will re-instate itself to 30 when the timer intervals.*/

    internal Int16 i = 30;

    private void timer1_Tick(object sender, EventArgs e)
    {

     /*For each tick of the timer, evaluate i and make sure it does not equal 0
     If it equals 0, time has run out (obviously :P ) and the "if" statement will
     pick it up and execute the code you want to execute when the timer is out.*/

        while(i != 0)
        {
         Answer.Visible = false;
         Feedback.Text = "Trop tard";
         timerBox.Text = Convert.ToString(i);
         i--;

         if(i = 0)
           {
           timer1.Enabled = false;
           //Any other things you want to happen or display when the time runs out
           }
        }
    }

    Keep in mind that I have not tested this code out in an actual compiler so you might have to tune it a bit. But its the general idea. Good luck!

    0
  • 0

    Actually I remember that Mike, while WOULD hang up the program. I dont have a compiler here with me at work, so I cant test this code out but Im positive Ive done it using a good ol' fashioned "tick". that was in VB though... Now I wanna see if I can remember how to do it. When I get home, C# Express, here I come!

    0
  • Mike Gold
    0
    Hi Brandon,

    you were close,  I think if you just removed the while loop and reset the count, you were good to go.
    0
  • 0
    I think I did it this way:

    public partial class Main : Form

    internal Int16 i = 30;
    TypeScript
    private void timer1_Tick(object sender, EventArgs e)
    {
        if(i != 0)
        {
         Answer.Visible = false;
         Feedback.Text = "Trop tard";
         timerBox.Text = Convert.ToString(i);
         i--;
        }
        else if(i == 0)
        {
        timer1.Enabled = false;
        i = 30;
        //Display whatever you want to when the timer runs out
        }
    }
    0
  • 0
    Great mike, it worked...

    Now another question, when i start it, the texbox with the countdown get the focus. How can i make it that the cursor starts in the answer textbox ?
    0
  • 0
    In the visual editor, double click the form and itll produce an onFormLoad event class in the code. Just put the following code in that event-

    answerTextBox.Focus();
    answerTextBox.SelectAll();

    Now when you start the form, it should focus on the answer text box.
    0
  • 0
    Awesome, thanx alot Brandom.

    Only thing, since that i had to click on a button to start the test or to get the next question, it was loosing focus so, i have inserted it in both button events.
    0
  • 0
    Your welcome... If you have anymore questions ask away! Im somewhat of a novice as well, but Im still familiar with alot of the basic concepts and Mike seems to be a really knowledgeable guy so we'll be sure to help you out.
    0
  • 0
    That's great,

    I want to learn it with as little help as possible tho.

    I am doing that only as an hobby.

    My goal is to create some apps to help my kids in their studies and also, create some little rpg starting with a text one for me and my son :)

    Then i will look into a tile based one and maybe, one day, one with nice graphics. But i might be in my 60's by that time lol !!
    0
  • 0
    Yeah, Ive found that it really works well when you get to mess with the code rather than just copying and pasting it. I just finished an address book program that writes, reads, updates, and deletes names and their information in an XML file it uses to store the data. Took me a pinch over two weeks but its done.
    My advice to you is to push yourself, say "I wonder if I can make it do this?" and try it, find ways to do it. Find creative work arounds and use different methods to do things. Itll really help you with it, it helped me alot!
    0