Example Application: Elementary Addition
This small application can help you check your ability to perform simple additions of natural numbers (integers between 0 and 99). Application…
2 min read341 words
Description#
This small application can help you check your ability to perform simple additions of natural numbers (integers between 0 and 99). Application: Introducing Text Boxes
- Create a Window Forms Application named ElementaryAddition2
- Design the form as follows: Control Text Name TextAlign Font Additional Properties Label 00 lblOperand1 Center Name: Tahoma Size: 48 Bold: True AutoSize: True ForeColor: Blue Label + Center Name: Arial Size: 50 Bold: True AutoSize: True ForeColor: Maroon Label 00 lblOperand2 Center Name: Tahoma Size: 48 Bold: True AutoSize: True ForeColor: Blue Label = Center Name: Arial Size: 50 Bold: True AutoSize: True ForeColor: Green TextBox 000 txtResult Center Name: Tahoma Size: 48 Bold: True Label New Operation lblNewOperation Center Name: Tahoma, Size: 28 Bold: True AutoSize: True BorderStyle: Fixed3D ForeColor: White BackColor:Maroon Label Check lblCheckAnswer Center Name: Tahoma, Size: 28 Bold: True AutoSize: True BorderStyle: Fixed3D ForeColor: White BackColor:Maroon Label Quit lblQuit Center Name: Tahoma, Size: 28 Bold: True AutoSize: True BorderStyle: FixedSingle
- Double-click the New Operation label and implement its event as follows: private void lblNewOperation_Click(object sender, EventArgs e) { int operand1; int operand2; Random rnd = new Random(); operand1 = rnd.Next(99); operand2 = rnd.Next(99); int result = operand1 + operand2; lblOperand1.Text = operand1.ToString(); lblOperand2.Text = operand2.ToString(); txtResult.Text = ""; txtResult.Focus(); }
- Return to the form and double-click the Check label
- Implement its event as follows: private void lblCheckAnswer_Click(object sender, EventArgs e) { int Operand1 = 0; int Operand2 = 0; int Result = 0; try { Operand1 = int.Parse(lblOperand1.Text); } catch (FormatException) { MessageBox.Show("Invalid Value"); } try { Operand2 = int.Parse(lblOperand2.Text); } catch (FormatException) { MessageBox.Show("Invalid Value"); } try { Result = int.Parse(txtResult.Text); } catch (FormatException) { MessageBox.Show("Invalid Answer"); } if (Result == (Operand1 + Operand2)) MessageBox.Show("WOW - Good Answer"); else MessageBox.Show("PSSST - Wrong Answer"); lblNewOperation_Click(sender, e); }
- Return to the form and double-click Quit
- Implement its event as follows: private void lblQuit_Click(object sender, EventArgs e) { Close(); }
- Execute the application and test it
- Click the Close button to close the form and return to your programming environment
Home Copyright © 2010-2011 C#Key

