Home

Example Application: Elementary Addition

Description

This small application can help you check your ability to perform simple additions of natural numbers (integers between 0 and 99).

Practical LearningPractical Learning: Introducing Text Boxes

  1. Create a Window Forms Application named ElementaryAddition2
  2. Design the form as follows:
     

    Elementary Addition

    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
  3. 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();
    }
  4. Return to the form and double-click the Check label
  5. 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);
    }
  6. Return to the form and double-click Quit
  7. Implement its event as follows:
    private void lblQuit_Click(object sender, EventArgs e)
    {
                Close();
    }
  8. Execute the application and test it
  9. Click the Close button to close the form and return to your programming environment

Home Copyright © 2010-2020, FunctionX