|
Windows Controls: Check Boxes |
|
|
A check box is a control that makes a statement true or
false. To perform this validation, the control displays a small square box
that the user can click. Here are examples:
|
To start, the square box may be empty .
If the user clicks it, a check mark appears in the square box
.
If the user clicks a check box that has a check mark in it, the check mark
may be removed.
To let the user know what the check box control
represents, the control is accompanied by a label that displays the
statement. When the square box is empty
,
the statement is false. When the square box is filled with a check mark
T, the statement is true.
Practical Learning: Introducing Check Boxes
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project
- In the middle list, click Windows Forms Application
- Change the Name to DaniloPizza1
- Click OK
- On the main menu, click View -> Other Windows -> Resource View.
In the Resource View, expand everything. Right-click Icon -> Insert
Icon
- Using the Erase tool
,
wipe its interior to erase its content
- Use the Ellipse tool
and the black color to draw an oval shape
- Fill
it up with a brown color (the color on the right side of the black)
- Click the red color
- Click the Airbrush tool
- Click the option button and select the Medium brush
- Randomly click different parts of the shape
- To give the appearance of a crust, use the Pencil tool
,
click the black color, right-click the brown color, and draw a dark
border around the shape
- With still the black and the brown colors, randomly click and
right-click different points in the shape:
- Click the white color. Right-click the yellow color. Randomly
click and right-click a few times in the shape
- Click the brown color again and click the Line tool
- Randomly draw horizontal, vertical, and dialog lines in the shape
- Right-click a white section in the drawing area, position the
mouse on Current Icon Image Types, and click 16x16, 16 Colors
- Design it as follows:
- Save the icon and close it
- Use the Icon field of the form in the Properties window to assign
the pizza.ico icon to the form
To support check boxes, the .NET Framework provides
the CheckBox class. To add a check box to your application at
design time, from the Common Controls section of the Toolbox, yon can
click the CheckBox control and click the form or a container on the form.
Unlike the radio button but like the regular command button, a check box
can safely be positioned directly on a form.
To programmatically create a check box, declare a
variable of type CheckBox, use the new operator to allocate memory
for it, and add it to the Controls collection of its holder. Here
is an example:
#include <windows.h>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class CExercise : public Form
{
private:
CheckBox ^ chkValidate;
public:
CExercise()
{
InitializeComponent();
}
private:
void InitializeComponent()
{
chkValidate = gcnew CheckBox;
Controls->Add(chkValidate);
}
};
[STAThread]
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Application::Run(gcnew CExercise);
return 0;
}
Unlike the radio button that usually comes along with
other radio buttons, a check box can appear by itself. Even when it comes
in a group with others, the behavior of one check box is independent of
the other check boxes, even if they belong to the same group.
Practical Learning: Adding Check Boxes
|
|
- Design the form as follows:
|
Control |
Text |
Name |
Additional Properties |
GroupBox |
|
Pizza Size |
|
|
RadioButton |
|
Small |
rdoSmall |
|
TextBox |
|
8.95 |
txtSmall |
AlignText: Right |
RadioButton |
|
Medium |
rdoMedium |
Checked: True |
TextBox |
|
10.75 |
txtMedium |
AlignText: Right |
RadioButton |
|
Large |
rdoLarge |
|
TextBox |
|
12.95 |
txtLarge |
AlignText: Right |
GroupBox |
|
Side Orders |
|
|
Label |
|
Qty |
|
|
Label |
|
Unit Price |
|
|
Label |
|
Sub Total |
|
|
Label |
|
Bread Sticks |
|
|
TextBox |
|
0 |
txtQtyBread |
AlignText: Right |
TextBox |
|
3.25 |
txtPriceBread |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalBread |
AlignText: Right ReadOnly: True |
Label |
|
Buffalo Wings |
|
|
TextBox |
|
0 |
txtQtyWings |
AlignText: Right |
TextBox |
|
2.15 |
txtPriceWings |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalWings |
AlignText: Right ReadOnly: True |
GroupBox |
|
Toppings |
|
|
CheckBox |
|
Pepperoni |
chkPepperoni |
|
CheckBox |
|
Sausage |
chkSausage |
|
CheckBox |
|
Extra Cheese |
chkExtraCheese |
|
CheckBox |
|
Olives |
chkOlives |
|
CheckBox |
|
Onions |
chkOnions |
|
Label |
|
Each Topping |
|
|
TextBox |
|
0.45 |
txtEachTopping |
AlignText: Right |
GroupBox |
|
Drinks |
|
|
Label |
|
Qty |
|
|
Label |
|
Unit Price |
|
|
Label |
|
Sub Total |
|
|
Label |
|
Soda Can |
|
|
TextBox |
|
0 |
txtQtyCan |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceCan |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalCan |
AlignText: Right ReadOnly: True |
Label |
|
Soda 20 Oz. |
|
|
TextBox |
|
0 |
txtQtySoda20 |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceSoda20 |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalSoda20 |
AlignText: Right ReadOnly: True |
Label |
|
Soda 2L Bottle |
|
|
TextBox |
|
0 |
txtQtySoda2L |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceSoda2L |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalSoda2L |
AlignText: Right ReadOnly: True |
Label |
|
Orange Juice |
|
|
TextBox |
|
0 |
txtQtyOJ |
AlignText: Right |
TextBox |
|
2.25 |
txtPriceOJ |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalOJ |
AlignText: Right ReadOnly: True |
Label |
|
Water |
|
|
TextBox |
|
0 |
txtQtyWater |
AlignText: Right |
TextBox |
|
1.25 |
txtPriceWater |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalWater |
AlignText: Right ReadOnly: True |
Button |
|
Close |
btnClose |
|
Label |
|
Total Price |
|
|
TextBox |
|
0.00 |
txtTotalPrice |
AlignRight: Right ReadOnly: True |
|
- Save everything
Check Box Characteristics
|
|
By default, a check box appears empty, which makes its
statement false. To make the statement true, the user can click it. There
are three main ways you can use this property of a check box. To select a
check box, you can set its Checked property to True. You can
also do it programmatically as follows:
void InitializeComponent()
{
chkValidate = gcnew CheckBox;
Controls->Add(chkValidate);
chkValidate->Checked = true;
}
To find out if an item is selected, get the value of
its Checked property. Another possibility consists of toggling the
state of the check mark. For example, you can check or uncheck the check
mark when the user clicks another button. To do this, you can simply
negate the truthfulness of the control as follows:
chkValidate->Checked = !chkValidate->Checked;
Like the radio button, when a check box is clicked,
the control fires a CheckedChanged event to let you know that it
has been checked. This event is of type EventArgs. If the check box
can display only two states, checked or unchecked, this event is enough.
Topic Applied: Configuring Check Boxes
|
|
- On the form, click the Small radio button
- Press and hold Shift
- Click the Medium radio button, the Large radio button, the
Pepperoni, the Sausage, the Extra Cheese, the Olives, and the Onions
check boxes
- Release Shift
- In the Properties window, click the Events button
- In the Events section, double-click Click
- Return to the form
- Click the txtSmall text box
- Press and hold Shift
- Click each of the following controls: txtMedium, txtLarge,
txtEachTopping, txtQtyBread, txtPriceBread, txtQtyWings,
txtPriceWince, txtQtyCan, txtPriceCan, txtQtySoda20, txtPriceSoda20,
txtQtySoda2L, txtPriceSoda2L, txtQtyOJ, txtPriceOJ, txtQtyWater, and
txtPriceWater
- Release Shift
- In the Events section of the Properties window, double-click Leave
- In the Code Editor, define a member function named
CalculatePrice() and implement the events as follows:
void CalculatePrice()
{
double PriceSize = 0.00;
double PriceEachTopping = 0.00, BWings, Bread,
SodaCan, Soda20, Soda2L, OJ, Water, PriceToppings, TotalOrder;
int Pepperoni, Sausage, ExtraCheese, Onions, Olives;
try {
// Get the price of pizza depending on the selected size
if( rdoSmall->Checked == true)
PriceSize = double::Parse(txtSmall->Text);
if( rdoMedium->Checked == true)
PriceSize = double::Parse(txtMedium->Text);
if( rdoLarge->Checked == true)
PriceSize = double::Parse(txtLarge->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you typed for the price of a pizza is invalid"
"\nPlease try again", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
// Get the price of a topping if it was selected
if( chkPepperoni->Checked == true)
Pepperoni = 1;
else
Pepperoni = 0;
if( chkSausage->Checked == true)
Sausage = 1;
else
Sausage = 0;
if( chkExtraCheese->Checked == true)
ExtraCheese = 1;
else
ExtraCheese = 0;
if( chkOnions->Checked == true)
Onions = 1;
else
Onions = 0;
if( chkOlives->Checked == true)
Olives = 1;
else
Olives = 0;
// Get the price of each topping
try {
PriceEachTopping = double::Parse(txtEachTopping->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you typed for the price of a each topping is invalid"
"\nPlease try again", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
PriceToppings = (Pepperoni + Sausage + ExtraCheese + Onions + Olives) * PriceEachTopping;
// Calculate the price of the side dishes
// depending on the quantity entered
BWings = double::Parse(txtTotalWings->Text);
Bread = double::Parse(txtTotalBread->Text);
// Calculate the price of the drink(s)
SodaCan = double::Parse(txtTotalCan->Text);
Soda20 = double::Parse(txtTotalSoda20->Text);
Soda2L = double::Parse(txtTotalSoda2L->Text);
OJ = double::Parse(txtTotalOJ->Text);
Water = double::Parse(txtTotalWater->Text);
TotalOrder = PriceSize + PriceToppings + BWings + Bread +
SodaCan + Soda20 + Soda2L + OJ + Water;
txtTotalOrder->Text = TotalOrder.ToString("F");
}
System::Void rdoSmall_Click(System::Object^ sender, System::EventArgs^ e)
{
CalculatePrice();
}
System::Void txtSmall_Leave(System::Object^ sender, System::EventArgs^ e)
{
int QtyBread = 0, QtyWings = 0, QtyCan = 0,
QtySoda20 = 0, QtySoda2L = 0, QtyOJ = 0, QtyWater = 0;
double PriceBread = 0.00, TotalBread = 0.00,
PriceWings = 0.00, TotalWings = 0.00,
PriceCan = 0.00, TotalCan = 0.00,
PriceSoda20 = 0.00, TotalSoda20 = 0.00,
PriceSoda2L = 0.00, TotalSoda2L = 0.00,
PriceOJ = 0.00, TotalOJ = 0.00,
PriceWater = 0.00, TotalWater = 0.00;
// Retrieve the quantity set in the text box
try {
QtyBread = int::Parse(txtQtyBread->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of bread sticks is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
// Get the unit price of the item
try {
PriceBread = double::Parse(txtPriceBread->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of bread sticks is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
// Calculate the sub-total of this item
TotalBread = QtyBread * PriceBread;
// Display the sub-total in the corresponding text box
txtTotalBread->Text = TotalBread.ToString("F");
try {
PriceWings = int::Parse(txtQtyWings->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of orders of buffalo wings is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceWings = double::Parse(txtPriceWings->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of buffalo wings is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalWings = QtyWings * PriceWings;
txtTotalWings->Text = TotalWings.ToString("F");
try {
QtyCan = int::Parse(txtQtyCan->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of soda cans is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceCan = double::Parse(txtPriceCan->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of soda cans is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalCan = QtyCan * PriceCan;
txtTotalCan->Text = TotalCan.ToString("F");
try {
QtySoda20 = int::Parse(txtQtySoda20->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of soda 20 Oz. is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceSoda20 = double::Parse(txtPriceSoda20->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of soda 20 Oz. is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalSoda20 = QtySoda20 * PriceSoda20;
txtTotalSoda20->Text = TotalSoda20.ToString("F");
try {
QtySoda2L = int::Parse(txtQtySoda2L->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of bottles of soda 2-litter is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceSoda2L = double::Parse(txtPriceSoda2L->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of a bottle of soda is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalSoda2L = QtySoda2L * PriceSoda2L;
txtTotalSoda2L->Text = TotalSoda2L.ToString("F");
try {
QtyOJ = int::Parse(txtQtyOJ->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of a bottle of orange juice is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceOJ = double::Parse(txtPriceOJ->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of bottles of orange is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalOJ = QtyOJ * PriceOJ;
txtTotalOJ->Text = TotalOJ.ToString("F");
try {
QtyWater = int::Parse(txtQtyWater->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the quantify of bottles of water is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
try {
PriceWater = double::Parse(txtPriceWater->Text);
}
catch(FormatException ^)
{
MessageBox::Show("The value you entered for the price of bottle of water is not valid"
"\nPlease try again!", "Danilo Pizza",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
TotalWater = QtyWater * PriceWater;
txtTotalWater->Text = TotalWater.ToString("F");
CalculatePrice();
}
- Return to the and double-click the Close button
- Implement its Click event as follows:
System::Void btnClose_Click(Object ^ sender, System.EventArgs ^ e)
{
Close();
}
- Execute the application and test the form. Here is an example:
- Close the form and return to your programming environment
|
|