Home

Example Application: Elementary Operations

Description

This is a small application that can let a kid practice such operations as the addition, the subtraction, the multiplication and the division. The user can also select a level of difficulty.

Practical LearningPractical Learning: Creating the Application

  1. Create a new Window Application named ElementaryOperations1
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Set the name to Exercise.cs
  4. Design the form as follows:
     

    Elementary Addition

    Control Text Name TextAlign Font Additional Properties
    Label Select Your Level        
    GroupBox          
    RadioButton Level 1 rdoLevel1 MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    Checked: TRue
    RadioButton Level 2 rdoLevel2 MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    RadioButton Level 3 rdoLevel3 MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    RadioButton Level 4 rdoLevel4 MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    Label Select the OPeration        
    GroupBox          
    RadioButton Addition rdoAddition MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    Checked: TRue
    RadioButton Subtraction rdoSubtraction MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    RadioButton Multiplication rdoMultiplication MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    RadioButton Division rdoDivision MiddleCenter Microsoft Sans Serif, 12pt, style=Bold Appearance: Button
    Label 000 lblOperand1 MiddleRight Microsoft Sans Serif, 54pt  
    Button New Operation btnNewOperation   Microsoft Sans Serif, 20pt  
    Label + lblOperation MiddleRight Microsoft Sans Serif, 54pt  
    Label 00 lblOperand2 MiddleRight Microsoft Sans Serif, 54pt  
    Panel         Size: 317, 10
    Label =   Center Microsoft Sans Serif, 54pt AutoSize: True
    ForeColor: Green
    TextBox 000 txtResult Right Microsoft Sans Serif, 54pt, style=Bold  
    Button Check Result btnCheckResult      
  5. Double-click the New Operation button and implement its event as follows:
    private void btnNewOPeration_Click(object sender, EventArgs e)
    {
                int Number1 = 0;
                int Number2 = 0;
                Random rndNumber = new Random();
    
                // If the user clicked the Level 1 button,
                // the operation will be performed on numbers from 1 to 9
                if (rdoLevel1.Checked == true)
                {
                    Number1 = rndNumber.Next(1, 10);
                    Number2 = rndNumber.Next(1, 10);
                }
                else if (rdoLevel2.Checked == true)
                {
                    // If the user clicked the Level 2 button,
                    // the operation will be performed on numbers from 10 to 19
                    Number1 = rndNumber.Next(10, 30);
                    Number2 = rndNumber.Next(10, 30);
                }
                else if (rdoLevel3.Checked == true)
                {
                    // If the user clicked the Level 3 button,
                    // the operation will be performed on numbers from 21 to 49
                    Number1 = rndNumber.Next(30, 50);
                    Number2 = rndNumber.Next(30, 50);
                }
                else if (rdoLevel4.Checked == true)
                {
                    // If the user clicked the Level 4 button,
                    // the operation will be performed on numbers from 51 to 99
                    Number1 = rndNumber.Next(50, 101);
                    Number2 = rndNumber.Next(50, 101);
                }
    
                // Display the numbers to the user
                lblOperand1.Text = Number1.ToString();
                lblOperand2.Text = Number2.ToString();
    
                // Just in case, empty the Result text box
                txtResult.Text = "";
                // Give focus to the Result text box
                txtResult.Focus();
    }
  6. Return to the form and click the Level 1 button
  7. Press and hold Shift
  8. On the form, click the Level 2, the Level 3, and the Level 4 buttons
  9. Release Shift
  10. In the Properties window, click Events and double-click Click 
  11. Implement the event as follows:
     
    private void rdoLevel1_Click(object sender, EventArgs e)
    {
                btnNewOPeration_Click(sender, e);
    }
  12. Return to the form and click the Addition button
  13. In the Events section of the Properties window, double-click Click and implement the event as follows:
     
    private void rdoAddition_Click(object sender, EventArgs e)
    {
                lblOperation.Text = "+";
    }
  14. Return to the form and click the Subtraction button
  15. In the Events section of the Properties window, double-click Click and implement the event as follows:
     
    private void rdoSubtraction_Click(object sender, EventArgs e)
    {
                lblOperation.Text = "-";
    }
  16. Return to the form and click the Multiplication button
  17. In the Events section of the Properties window, double-click Click and implement the event as follows:
     
    private void rdoMultiplication_Click(object sender, EventArgs e)
    {
                lblOperation.Text = "*";
    }
  18. Return to the form and click the Division button
  19. In the Events section of the Properties window, double-click Click and implement the event as follows:
     
    private void rdoDivision_Click(object sender, EventArgs e)
    {
                lblOperation.Text = "/";
    }
  20. Return to the form and double-click the Check Result button
  21. Implement the event as follows:
    private void btnCheckResult_Click(object sender, EventArgs e)
    {
        double Number1, Number2;
        double UserResult, OurResult;
        Random RandomNumber = new Random();
    
        // It is hard to perform a comparison on a division
        // So we will have to do some gymastic here to get something
        // We will use this variable to format the number 
        // to appear as 0.00
        // That will allow us to perform the comparision
        // on a decimal number with a precision of 2
        string strFixedResult;
        strFixedResult = "";
    
        string[] Congratulations = new string[]{ 
                "Right :) - WOW - Good Answer!",
                "Good Answer :) - You are Impressive", 
                "Right Answer :) - What a Good Job!", 
                "Good :) - You Are Greaaaaaaaaaaaat", 
                "Wonderful Answer :) - You Know It" 
                };
        string[] WrongAnswers = { 
                "Uhhhhhhhhhh - Bad Answer", 
                "Wrong - You will do better next time", 
                "Nop", 
                "Common - You can do Better Than That!",
                "No - You are probably getting tired" 
                };
    
        // Make sure the user provides a result
        if (txtResult.Text == "")
        {
            MessageBox.Show("You must provide a result before clicking the button");
            return;
        }
    
        // Use exception handling to get the result
        UserResult = double.Parse(txtResult.Text);
    
        Number1 = double.Parse(lblOperand1.Text);
        Number2 = double.Parse(lblOperand2.Text);
    
        // Get the user's answer
        if (rdoAddition.Checked == true)
        {
            OurResult = Number1 + Number2;
            // Format the result to appear with 2 decimal numbers
            strFixedResult = string.Format("{0:F}", Number1 + Number2);
        }
    
        if (rdoSubtraction.Checked == true)
        {
            OurResult = Number1 - Number2;
            strFixedResult = string.Format("{0:F}", Number1 - Number2);
        }
    
        if (rdoMultiplication.Checked == true)
        {
            OurResult = Number1 * Number2;
            strFixedResult = string.Format("{0:F}", Number1 * Number2);
        }
    
        if (rdoDivision.Checked == true)
        {
            OurResult = Number1 / Number2;
            strFixedResult = string.Format("{0:F}", Number1 / Number2);
        }
    
        // Check if the user's answer is the right one
        // Because of the division, we will format the result as 0.00
        // then perform the comparison
        if (strFixedResult == double.Parse(txtResult.Text).ToString("F"))
            MessageBox.Show(Congratulations[RandomNumber.Next(0, 4)],
                            "Elementary Operations",
                            MessageBoxButtons.OK, MessageBoxIcon.Question);
        else
        {
            MessageBox.Show(WrongAnswers[RandomNumber.Next(0, 4)],
                            "Elementary Operations",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        // After checking the user's answer, generate a new operation
        btnNewOPeration_Click(sender, e);
    }
  22. Execute the application and test it
     
  23. Close the form and return to your programming environment

Home Copyright © 2010-2020, FunctionX