Home

Control-Based Application: Boolean Algebra

 

Introduction

This application is used to explore the appearance characteristic of a check box. To do this, it simulates the logical conjunction and the logical disjunction of Boolean algebra.

Windows Controls Used:

Practical Learning Practical Learning: Creating the Application

  1. Start Microsoft Visual C# and create a new Windows Forms Application Logic1
  2. Design the form as follows:
     
    Logic
    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    
  3. 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";
    }
  4. Return to the form
  5. 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";
    }
  6. Return to the form
  7. 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";
    }
  8. Return to the form
  9. 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)];
    
  10. Return to the form
  11. 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!");
                    }
                }
            }
        }
    }
  12. Return to the form
  13. Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
            Close();
    }
  14. Return to the form
 
 

Home Copyright 2008-2009, yevol.com