Translate

Microsoft Technologies with C# and Dot Net

The Maths Quiz Code( in C#) (S. Mandgi)

What i have done is picked up and modified a C# code from Microsoft which is a Maths's Quiz for primary school kids . Every instance you run the program new random problems are generated. To build the program you need the Microsoft C# 2010 Visual Studio and a Dot Net environment on your 32 bit Windows OS.

Included in here is a video to show you how it works . You need to have a flash player installed.
video

----------------------------------------------------------------------------------------------------
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();

        // These ints will store the numbers
        // for the addition problem.
        int addend1;
        int addend2;

        // These ints will store the numbers
        // for the subtraction problem.
        int minuend;
        int subtrahend;

        // These ints will store the numbers for the multiplication problem.
        int multiplicand;
        int multiplier;

        // These ints will store the numbers for the division problem.
        int dividend;
        int divisor;

        // This int will keep track of the time left.
        int timeLeft;

        /// <br />
<summary>
        /// Start the quiz by filling in all of the problems
        /// and starting the timer.
        /// </summary>
        public void StartTheQuiz()
        {
            // Fill in the addition problem.
            addend1 = randomizer.Next(51);
            addend2 = randomizer.Next(51);

            plusLeftLabel.Text = addend1.ToString();
            plusRightLabel.Text = addend2.ToString();

            sum.Value = 0;

            // Fill in the subtraction problem.
            minuend = randomizer.Next(1, 101);
            subtrahend = randomizer.Next(1, minuend);
            minusLeftLabel.Text = minuend.ToString();
            minusRightLabel.Text = subtrahend.ToString();
            difference.Value = 0;

            // Fill in the multiplication problem.
            multiplicand = randomizer.Next(2, 11);
            multiplier = randomizer.Next(2, 11);
            timesLeftLabel.Text = multiplicand.ToString();
            timesRightLabel.Text = multiplier.ToString();
            product.Value = 0;

            // Fill in the division problem.
            divisor = randomizer.Next(2, 11);
            int temporaryQuotient = randomizer.Next(2, 11);
            dividend = divisor * temporaryQuotient;
            dividedLeftLabel.Text = dividend.ToString();
            dividedRightLabel.Text = divisor.ToString();
            quotient.Value = 0;
            sum.ReadOnly = false;
            difference.ReadOnly = false;
            product.ReadOnly= false;
            quotient.ReadOnly = false;

            // Start the timer.
            timeLeft = 60;
            timeLabel.Text = "60 seconds";
            timer1.Start();

        }

        private bool CheckTheAnswer()
        {
            if (((addend1 + addend2) == sum.Value)
                &amp;&amp; ((minuend - subtrahend) == difference.Value)
                &amp;&amp; ((multiplicand * multiplier) == product.Value)
                &amp;&amp; ((dividend / divisor) == quotient.Value))
                return true;
            else
                return false;

            if ((addend1 + addend2) != sum.Value)
            {
                int wrongsumValue;
                wrongsumValue = addend1 + addend2;

            }
            if ((minuend - subtrahend) != difference.Value)
            {
                int wrongdiffValue;
                wrongdiffValue = minuend - subtrahend;
            }
            if ((multiplicand * multiplier) != product.Value)
            {
                int wrongproductValue;
                wrongproductValue = multiplicand * multiplier;
            }
            if ((dividend / divisor) != quotient.Value)
            {
                int wrongquotientValue;
                wrongquotientValue = dividend / divisor;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void plusRightLabel_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void startButton_Click(object sender, EventArgs e)
        {
            StartTheQuiz();
            startButton.Enabled = false;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            CheckTheAnswer();
            if (CheckTheAnswer())
            {
             
                // If the user got the answer right, stop the timer
                // and show a MessageBox.
                timer1.Stop();
                MessageBox.Show("You got all the answers right!",
                                "Congratulations");
                startButton.Enabled = true;
                sum.ReadOnly = true;
                difference.ReadOnly = true;
                product.ReadOnly = true;
                quotient.ReadOnly = true;
            }

            else
            {
                // Decrease the time left by one second and display
                // the new time left by updating the Time Left label.
                timeLeft--;
                timeLabel.Text = timeLeft + " seconds";
                if (timeLeft &lt; 16)
                {
                    timeLabel.BackColor = Color.Red;
                }
                // Reset time display


                if (timeLeft &lt; 1)
                {
                    // If the user ran out of time, stop the timer, show
                    // a MessageBox, and fill in the answers.
                    timer1.Stop();
                    timeLabel.Text = "Time's up!";
                    MessageBox.Show("You didn't finish in time.", "Sorry");
                }

                startButton.Enabled = true;


            }
        }

        private void answer_Enter(object sender, EventArgs e)
        {
            // Select the whole answer in the NumericUpDown control.
            NumericUpDown answerBox = sender as NumericUpDown;

            if (answerBox != null)
            {
                int lengthOfAnswer = answerBox.Value.ToString().Length;
                answerBox.Select(0, lengthOfAnswer);
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            CheckTheAnswer();
            if (CheckTheAnswer())
            {
                // If the user got the answer right, stop the timer
                // and show a MessageBox.
                MessageBox.Show("You got all the answers right!",
                                "Congratulations");
            startButton.Enabled = true;
            sum.Visible = true;
            difference.Visible = true;
            product.Visible = true;
            quotient.Visible = true;
        }
         

            else
            {
                MessageBox.Show("You got some or all the answers wrong!",
                                "Press Start to take the Quiz once more ?");
                startButton.Enabled = true;
                sum.Visible = true;
                difference.Visible = true;
                product.Visible = true;
                quotient.Visible = true;
            }
                     
        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }
    }
}

____________________________________________________________________________
____________________________________________________________________________