The CheckBox control for Windows Forms indicates whether a specific condition is on or off. Usually it is used to present the user with a Yes / No or True / False selection. We can use group check box controls to show multiple choices from which the user can choose one or more. It is similar to the RadioButton
control, but any number of grouped CheckBox controls may be selected.
The control of the CheckBox may display an image or text or both. Usually CheckBox comes with a caption, which we can set in the Text
property.
checkBox1.Text = "Black";
We can use the CheckBox control ThreeState
property to direct the control to return the Checked, Unchecked, and Indeterminate values. We need to set the check boxs ThreeState
property to True to indicate that we want it to support three states.
checkBox1.ThreeState = true;
For various functions, the Radio Button and the Check Box are used. Use a radio button when we want the user to choose only one option. on the other hand when we want the user to choose all appropriate options, use a Check Box.
The following C# program shows a mesage box when user check Check Box (name chkBlack) .
if(chkBlack.Checked==true)
{
MessageBox.Show("Black Check Box is checked. Then red check box also checked.");
chkRed.Checked = true;
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CheckBoxControl
{
public partial class frmCheckBox : Form
{
public frmCheckBox()
{
InitializeComponent();
}
private void chkBlack_CheckedChanged(object sender, EventArgs e)
{
lblColorDisplay.BackColor = Color.Black;
}
private void chkRed_CheckedChanged(object sender, EventArgs e)
{
lblColorDisplay.BackColor = Color.Red;
}
private void chkGreen_CheckedChanged(object sender, EventArgs e)
{
lblColorDisplay.BackColor = Color.Green;
}
private void btnCheck_Click(object sender, EventArgs e)
{
if(chkBlack.Checked==true)
{
MessageBox.Show("Black Check Box is checked. Then red check box also checked.");
chkRed.Checked = true;
}
else
{
MessageBox.Show("Black check box not checked. Then red check box also not checked.");
chkRed.Checked = false;
}
}
}
}