Home

Windows Controls: Check Boxes

 

Introduction

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:

The Options dialog box of Microsoft FrontPage

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.

Creating a Check Box

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:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    CheckBox chkValidate;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        chkValidate = new CheckBox();

        Controls.Add(chkValidate);
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        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 Practical Learning: Introducing Check Boxes

  1. Start a new Windows Application named DaniloPizza1
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Type Exercise.cs and press Enter
  4. On the main menu, click Project -> Add New Item...
  5. In the Templates list, click Icon File
  6. Set the Name to pizza and click Add
  7. Using the Erase tool Erase, wipe its interior to erase its content
  8. Use the Ellipse tool and the black color to draw an oval shape
     
  9. Fill Fill it up with a brown color (the color on the right side of the black)
  10. Click the red color
  11. Click the Airbrush tool
  12. Click the option button and select the Medium brush
     
    Medium Air Brush
  13. Randomly click different parts of the shape
     
  14. 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
  15. With still the black and the brown colors, randomly click and right-click different points in the shape:
     
  16. Click the white color. Right-click the yellow color. Randomly click and right-click a few times in the shape
  17. Click the brown color again and click the Line tool
  18. Randomly draw horizontal, vertical, and dialog lines in the shape
     
  19. Right-click a white section in the drawing area, position the mouse on Current Icon Image Types, and click 16x16, 16 Colors
  20. Design it as follows:
     
  21. Save the icon and close it
  22. Use the Icon field of the form in the Properties window to assign the pizza.ico icon to the form
  23. Design the form as follows:
     
    Danilo Pizza - Design
     
    Control Text Name Additional Properties
    GroupBox GroupBox Pizza Size    
    RadioButton RadioButton Small rdoSmall  
    TextBox TextBox 8.95 txtSmall AlignText: Right
    RadioButton RadioButton Medium rdoMedium Checked: True
    TextBox TextBox 10.75 txtMedium AlignText: Right
    RadioButton RadioButton Large rdoLarge  
    TextBox TextBox 12.95 txtLarge AlignText: Right
    GroupBox GroupBox Side Orders    
    Label Label Qty    
    Label Label Unit Price    
    Label Label Sub Total    
    Label Label Bread Sticks    
    TextBox TextBox 0 txtQtyBread AlignText: Right
    TextBox TextBox 3.25 txtPriceBread AlignText: Right
    TextBox TextBox 0.00 txtTotalBread AlignText: Right
    ReadOnly: True
    Label Label Buffalo Wings    
    TextBox TextBox 0 txtQtyWings AlignText: Right
    TextBox TextBox 2.15 txtPriceWings AlignText: Right
    TextBox TextBox 0.00 txtTotalWings AlignText: Right
    ReadOnly: True
    GroupBox GroupBox Toppings    
    CheckBox CheckBox Pepperoni chkPepperoni  
    CheckBox CheckBox Sausage chkSausage  
    CheckBox CheckBox Extra Cheese chkExtraCheese  
    CheckBox CheckBox Olives chkOlives  
    CheckBox CheckBox Onions chkOnions  
    Label Label Each Topping    
    TextBox TextBox 0.45 txtEachTopping AlignText: Right
    GroupBox GroupBox Drinks    
    Label Label Qty    
    Label Label Unit Price    
    Label Label Sub Total    
    Label Label Soda Can    
    TextBox TextBox 0 txtQtyCan AlignText: Right
    TextBox TextBox 1.45 txtPriceCan AlignText: Right
    TextBox TextBox 0.00 txtTotalCan AlignText: Right
    ReadOnly: True
    Label Label Soda 20 Oz.    
    TextBox TextBox 0 txtQtySoda20 AlignText: Right
    TextBox TextBox 1.45 txtPriceSoda20 AlignText: Right
    TextBox TextBox 0.00 txtTotalSoda20 AlignText: Right
    ReadOnly: True
    Label Label Soda 2L Bottle    
    TextBox TextBox 0 txtQtySoda2L AlignText: Right
    TextBox TextBox 1.45 txtPriceSoda2L AlignText: Right
    TextBox TextBox 0.00 txtTotalSoda2L AlignText: Right
    ReadOnly: True
    Label Label Orange Juice    
    TextBox TextBox 0 txtQtyOJ AlignText: Right
    TextBox TextBox 2.25 txtPriceOJ AlignText: Right
    TextBox TextBox 0.00 txtTotalOJ AlignText: Right
    ReadOnly: True
    Label Label Water    
    TextBox TextBox 0 txtQtyWater AlignText: Right
    TextBox TextBox 1.25 txtPriceWater AlignText: Right
    TextBox TextBox 0.00 txtTotalWater AlignText: Right
    ReadOnly: True
    Button Button Close btnClose  
    Label Label Total Price    
    TextBox TextBox 0.00 txtTotalPrice AlignRight: Right
    ReadOnly: True
  24. Save everything
 

Home Copyright © 2007 FunctionX, Inc. Next