 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 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: 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();
}```
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);
}```
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 = "+";
}```
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 = "-";
}```
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 = "*";
}```
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 = "/";
}```
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"
};
"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);

{
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
{   