Home

Example Application: Boolean Algebra

 

Checking a Check Box

The check box of Microsoft Windows is a logical control that specifies the true or false value of a statement. In this application, we use this feature to evaluate the logical conjunction and disjunction of Boolean Algebra.

This application uses the ^ symbol for logical conjunction and the V letter for logical disjunction.

Windows Controls:

Practical Learning Practical Learning: Using the Appearance of a Check Box

  1. Start Microsoft Visual C#
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the Templates list, click Windows Application and set the name to BooleanAlgebra1
  4. Click OK
  5. Design the form as follows:
     
    Control Text Name Font FlatStyle Appearance
    Label A   Microsoft Sans Serif, 24pt    
    Label B   Microsoft Sans Serif, 24pt    
    Label A ^ B lblOperation Microsoft Sans Serif, 24pt    
    CheckBox False chkOperand1 Microsoft Sans Serif, 18pt System Button
    CheckBox False chkOperand2 Microsoft Sans Serif, 18pt System Button
    CheckBox False chkResult Microsoft Sans Serif, 18pt System Button
    Button New Operation btnNewOperation Microsoft Sans Serif, 15.75pt    
    Button Check btnCheckOperation Microsoft Sans Serif, 18pt    
    Button Close btnClose Microsoft Sans Serif, 15.75pt    
  6. Double-click the top-left check box and implement its event as follows:
     
    private void chkOperand1_CheckedChanged(object sender, EventArgs e)
    {
            if (chkOperand1.Checked == true)
                    chkOperand1.Text = "True";
            else
                    chkOperand1.Text = "False";
    }
  7. Return to the form
  8. Double-click the top-center check box and implement its event as follows:
     
    private void chkOperand2_CheckedChanged(object sender, EventArgs e)
    {
            if (chkOperand2.Checked == true)
                    chkOperand2.Text = "True";
            else
                    chkOperand2.Text = "False";
    }
  9. Return to the form
  10. Double-click the top-right check box and implement its event as follows:
     
    private void chkResult_CheckedChanged(object sender, EventArgs e)
    {
            if (chkResult.Checked == true)
                    chkResult.Text = "True";
            else
                    chkResult.Text = "False";
    }
  11. Return to the form
  12. Double-click the New Operation button and implement its event as follows:
     
    private void btnNewOperation_Click(object sender, EventArgs e)
    {
            string[] strBooleanValues = { "True", "False" };
            string[] strOperations = { "A ^ B", "A V B" };
            Random rnd = new Random();
    
            chkOperand1.Text = strBooleanValues[rnd.Next(2)];
            chkOperand2.Text = strBooleanValues[rnd.Next(2)];
    
            if (chkOperand1.Text == "True")
                    chkOperand1.Checked = true;
            else
                    chkOperand1.Checked = false;
    
            if (chkOperand2.Text == "True")
                    chkOperand2.Checked = true;
            else
                    chkOperand2.Checked = false;
    
            lblOperation.Text = strOperations[rnd.Next(2)];
    
  13. Return to the form
  14. Double-click the Check button and implement its event as follows:
     
    private void btnCheckResult_Click(object sender, EventArgs e)
    {
            // Logical Conjunction
            if (lblOperation.Text == "A ^ B")
            {
                    // If A = true
                    if (chkOperand1.Checked == true)
                    {
                        if (chkOperand2.Checked == true)
                        {
                            if (chkResult.Checked == true)
                            {
                                // | A | B | A ^ B |
                                // | T | T |   T   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else  if (chkResult.Checked == false)
                            {
                                // | A | B | A ^ B |
                                // | T | T |   F   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                        else if (chkOperand2.Checked == false)
                        {
                            if (chkResult.Checked == false)
                            {
                                // | A | B | A ^ B |
                                // | T | F |   F   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else  if (chkResult.Checked == true)
                            {
                                // | A | B | A ^ B |
                                // | T | F |   T   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                    }
                    else  if( chkOperand1.Checked == false )
                    {
                        if (chkOperand2.Checked == true)
                        {
                            if (chkResult.Checked == false)
                            {
                                // | A | B | A ^ B |
                                // | F | T |   F   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else  if (chkResult.Checked == true )
                            {
                                // | A | B | A ^ B |
                                // | F | T |   T   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                        else  if (chkOperand2.Checked == false)
                        {
                            if (chkResult.Checked == false)
                            {
                                // | A | B | A ^ B |
                                // | F | F |   F   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else  if (chkResult.Checked == true)
                            {
                                // | A | B | A ^ B |
                                // | F | F |   T   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                    }
                }
                else if (lblOperation.Text == "A V B") // Logical Disjunction:
                {
                    // If A = true
                    if (chkOperand1.Checked == true)
                    {
                        if (chkOperand2.Checked == true)
                        {
                            if (chkResult.Checked == true)
                            {
                                // | A | B | A V B |
                                // | T | T |   T   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else if (chkResult.Checked == false)
                            {
                                // | A | B | A V B |
                                // | T | T |   F   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                        else if (chkOperand2.Checked == false)
                        {
                            if (chkResult.Checked == true)
                            {
                                // | A | B | A V B |
                                // | T | F |   T   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else if (chkResult.Checked == false)
                            {
                                // | A | B | A V B |
                                // | T | F |   F   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                    }
                    else if (chkOperand1.Checked == false)
                    {
                        if (chkOperand2.Checked == true)
                        {
                            if (chkResult.Checked == true)
                            {
                                // | A | B | A V B |
                                // | F | T |   T   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else if (chkResult.Checked == false)
                            {
                                // | A | B | A V B |
                                // | F | T |   F   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                        else if (chkOperand2.Checked == false)
                        {
                            if (chkResult.Checked == false)
                            {
                                // | A | B | A V B |
                                // | F | F |   F   |
                                MessageBox.Show("Bravo - Good Answer");
                            }
                            else if (chkResult.Checked == true)
                            {
                                // | A | B | A V B |
                                // | F | F |   T   |
                                MessageBox.Show("Wrong - Get it right next time!");
                            }
                        }
                    }
            }
    }
  15. Return to the form
  16. Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
            Close();
    }
  17. Return to the form
 

Copyright © 2007 FunctionX, Inc. Home