Example Application: Boolean Algebra

Description

This example explores the button side of a check box. The application is an exercise on Boolean algebra (it includes only the AND and the OR operations).

Practical Learning: Creating the Application

1. Start Microsoft Visual Studio
2. To start a new application, on the main menu, click File -> New Project...
3. In the middle list, click Windows Application
4. Set the name to BooleanAlgebra1
5. Click OK
6. 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
7. 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";
}```
9. 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";
}```
11. 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";
}```
13. 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)];
```
15. 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   |
}
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   |
}
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   |
}
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   |
}
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   |
}
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   |
}
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   |
}
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   |
}
else if (chkResult.Checked == true)
{
// | A | B | A V B |
// | F | F |   T   |
MessageBox.Show("Wrong - Get it right next time!");
}
}
}
}
}```
```private void btnClose_Click(object sender, EventArgs e)