Visual C#

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…

4 min read777 words1 code sample

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:

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:

C#
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: 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 , wipe its interior to erase its content
  8. Use the Ellipse tool and the black color to draw an oval shape
  9. 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
  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: 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
  24. Save everything

Home Copyright © 2008 C# Key Next