|
Example Application: Boolean Algebra |
|
|
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).
|
Application:
Creating the Application
|
|
- Start Microsoft Visual Studio
- To start a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Application
- Set the name to
BooleanAlgebra1
- Click OK
- 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 |
|
|
|
- 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";
}
- Return to the form
- 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";
}
- Return to the form
- 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";
}
- Return to the form
- 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)];
- Return to the form
- 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!");
}
}
}
}
}
- Return to the form
- Double-click the Close button and implement its event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Return to the form
|
|