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
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 |
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(); }
private void rdoLevel1_Click(object sender, EventArgs e) { btnNewOPeration_Click(sender, e); }
private void rdoAddition_Click(object sender, EventArgs e) { lblOperation.Text = "+"; }
private void rdoSubtraction_Click(object sender, EventArgs e) { lblOperation.Text = "-"; }
private void rdoMultiplication_Click(object sender, EventArgs e) { lblOperation.Text = "*"; }
private void rdoDivision_Click(object sender, EventArgs e) { lblOperation.Text = "/"; }
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); }
|
||
Home | Copyright © 2010-2020, FunctionX | |
|