Casey Clark

Casey Clark

  • NA
  • 1
  • 0

Simple beginner's project in C# using timer control

Feb 1 2010 1:25 PM
greetings everyone, I am trying to write an assignment program that puts a time limit on a math game, if the awnser is timed out the program needs to display "incorrect" as it would for a wrong awnser as well as reset the timer response cycle each time next question is clicked. I've got some of the code written here. I'm a noob to C# :P

any help? thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MathGame
{
    public partial class Form1 : Form
    {
        Random randomNo = new Random();//initializes variables
        int no1;
        int no2;
        int TimeLeft = 30;
        String problemType = "+";
         public Form1()
        {
            InitializeComponent();
        }

        private void StartGame_Click(object sender, EventArgs e)
        {
            TimeLabel.Visible = true;
            StartGame.Visible = false;
            CheckAnswer.Visible = true;
            Question.Visible = true;
            Answer.Visible = true;
            NextQuestion.Visible = true;
            Feedback.Visible = true;
            timer1.Start();
             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 + "=";
        }//end Gen_Question method

        private void CheckAnswer_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (problemType == "+")//if else statements calculate and compare user entered answers and determine if they are correct or not
            {
                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";
            }
        }//end control statements

        private void NextQuestion_Click(object sender, EventArgs e)
        {
            Gen_Question();
            Answer.Text = "";
            Feedback.Text = "";
        }//generates another question and clears answer and feedback labels
        

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (TimeLeft > 0)
            {
                TimeLeft = TimeLeft - 1;
                TimeLabel.Text = TimeLeft + "seconds";
            }
            else
            {
                timer1.Stop();
                TimeLabel.Text = "Time is Up!";
                Feedback.Text = "Incorrect";
            }
        }
        }
    }



Answers (2)