.Net Controls: CheckBox


 

Introduction

A check box is a control that makes a statement true or false. To perform this validation, this control displays a small square box that the user can click. To start, the square box is empty *. If the user clicks it, a check mark appears in the square box T. 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.

Check Box Example
  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Check Box
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Windows Controls";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  12. Test the application.

Creating a Check Box

To create a check box control, you can use the CheckBox class.

  1. To create a check box, declare a pointer to CheckBox. You can use the Location property to specify the position of the control. Don't forget to specify the label of the check box, which is done using the Text property.
    As an example, change the file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// Declare a pointer to the CheckBox class
    	CheckBox *ExtraCheese;
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Windows Controls";
    
    	// Create the check box
    	ExtraCheese = new CheckBox;
    	ExtraCheese->Location = Point(16, 16);
    	ExtraCheese->Text = S"&Extra Cheese";
    	// After creating a check box, add it to the collection of 
    	// controls of the form
    	this->Controls->Add(ExtraCheese);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  2. Test the application
     
    Check Box

 

Check Box Properties

By default, a check box appears empty, which, as we saw in the description, makes its statement false. To make the statement true, the user clicks it. There are three main ways you can use this property of a check box. To select a check box, set its Checked property to 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 with regards to another action. 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:

ExtraCheese->Checked = !ExtraCheese->Checked;

By default, the square box of a check box control is positioned to the left side of its accompanying label. In Microsoft .Net applications, you have many options. Besides the left position, the most common alignment consists of positioning the round box to the right side of its label. The position of the round box with regards to its label is controlled by the CheckAlign property. The possible values are: TopLeft, TopCenter, TopRight, MiddleRight, BottomRight, BottomCenter, and BottomLeft.

Instead of being definitely checked, you can let the user know that the decision of making the statement true or false is not complete. To do this, a check box can display as "half-checked". In this case the check mark would appear as if it were disabled. This behavior is controlled through the CheckState property. To provide this functionality, assign the Indeterminate value to its CheckState property as follows:

ExtraCheese->CheckState = CheckState::Indeterminate;
Indeterminate check box

The CheckState property only allows setting the check box control as "undecided". If you actually want the user to control three states of the control as checked, half-checked, or unchecked, use the ThreeState property. By setting this Boolean property to true, the user can click it two to three times to get the desired value. When this ability is given to the user, then you can use or check the value of the Indeterminate property to find out whether the control is checked, half-checked, or unchecked. Here is an example of specifying the check box control as being able to display one of three states:

SimpleForm::SimpleForm()
{
	this->Text = S"Windows Controls";

	// Create the check box
	ExtraCheese = new CheckBox;
	ExtraCheese->Location = Point(16, 16);
	ExtraCheese->Text = S"&Extra Cheese";
	ExtraCheese->Checked = true;
	ExtraCheese->ThreeState = true;
	// After creating a check box, add it to the collection of 
	// controls of the form
	this->Controls->Add(ExtraCheese);
}

By default, a check box control appears as a square box that gets filled with a check mark when the user clicks it. Optionally, you can make a check box control appear as a toggle button. In that case, the button would appear as a regular button. When the user clicks it, it appears down. If the user clicks it again, it becomes up. To change the appearance of a check box, assign the Button or Normal value to its Appearance property. The Appearance values are defined in the Appearance namespace. Here is an example:

SimpleForm::SimpleForm()
{
	this->Text = S"Windows Controls";

	// Create the check box
	ExtraCheese = new CheckBox;
	ExtraCheese->Location = Point(16, 16);
	ExtraCheese->Text = S"&Extra Cheese";
	ExtraCheese->Appearance = Appearance::Button;
	// After creating a check box, add it to the collection of 
	// controls of the form
	this->Controls->Add(ExtraCheese);
}

 

  1. To make the check box true, that is, to select it at startup, in the constructor of the form, set the Checked value of the control to true:
     
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Windows Controls";
    
    	// Create the check box
    	ExtraCheese = new CheckBox;
    	ExtraCheese->Location = Point(16, 16);
    	ExtraCheese->Text = S"&Extra Cheese";
    	ExtraCheese->Checked = true;
    	// After creating a check box, add it to the collection of 
    	// controls of the form
    	this->Controls->Add(ExtraCheese);
    }
    Check box whose label is aligned to the right
  2. To position the square box to the right of the label, change the creation of the control as follows:
     
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Windows Controls";
    
    	// Create the check box
    	ExtraCheese = new CheckBox;
    	ExtraCheese->Location = Point(16, 16);
    	ExtraCheese->Text = S"&Extra Cheese";
    	ExtraCheese->Checked = true;
    	ExtraCheese->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	// After creating a check box, add it to the collection of 
    	// controls of the form
    	this->Controls->Add(ExtraCheese);
    }
  3. Test the application
     
    Check box with CheckAlign

Check Box Events

The most obvious, the most common, and the most regular event of a check box, just like any button, is the OnClick event. This event fires when the user clicks the radio button.

  1. To implement the OnClick event of the check box control, change the file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// Declare a pointer to the CheckBox class
    	CheckBox *ExtraCheese;
    	void ExtraCheeseClick(Object *Sender, EventArgs *Args);
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Windows Controls";
    
    	// Create the check box
    	ExtraCheese = new CheckBox;
    	ExtraCheese->Location = Point(16, 16);
    	ExtraCheese->Text = S"&Extra Cheese";
    	ExtraCheese->Checked = true;
    	ExtraCheese->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	ExtraCheese->ThreeState = true;
    	ExtraCheese->add_Click(new EventHandler(this, ExtraCheeseClick));
    	// After creating a check box, add it to the collection of 
    	// controls of the form
    	this->Controls->Add(ExtraCheese);
    }
    
    void SimpleForm::ExtraCheeseClick(Object *Sender, EventArgs *Args)
    {
    	// Change the form's caption when the user clicks the control
    	this->Text = S"Selected";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  2. Test the application
  3. To find out the state of the check box when the user clicks the control, change the event as follows:
     
    void SimpleForm::ExtraCheeseClick(Object *Sender, EventArgs *Args)
    {
    	// Change the form's caption when the user clicks the control
    	if( ExtraCheese->CheckState == CheckState::Checked )
    		this->Text = S"Check Box Selected";
    	else if( ExtraCheese->CheckState == CheckState::Indeterminate )
    		this->Text = S"Can't Decide";
    	else if( ExtraCheese->CheckState == CheckState::Unchecked )
    		this->Text = S"Empty Life";
    }
  4. Test the application
     
    Check box
 

Home Copyright © 2002 FunctionX, Inc.