C# – How to use CheckBox control?

CheckBox control is useful user interface element, used to get an option (true / false or yes / no) from the user. You might be noticed these kind of controls, commonly in Browser settings, an Application settings etc.,

The control has one checkbox displayed along with the text; and the user can select or unselect the checkbox. CheckBox control has two or three states. One is select state, other one is not-selected state and another state is indeterminate state. These states can be controlled using appropriate properties & events of the control; which we are going to discuss in below section(s).

Properties

Control’s Checked property will set to True or False depending on whether we select or unselect the control. By default, it has two states, one is select or checked state (True) and other one is not-selected or unchecked state (False). When it has two states; we call it as two-state control.

By setting ThreeState property to True, we can add a third state; which is indeterminate state, to the control. We call it as three-state control. Usually, Checked property will be used for the two-state control and CheckState property is used for three-state control; it’s values are set to Checked, Unchecked or Indeterminate depending on the user selection.

We can use Checked or CheckState properties, depending on the control type. For two-state control, Checked property is set to True or False and CheckState property is set to either Checked or Unchecked.

For three-state control, Checked property is set to True when CheckState property value is Checked or Indeterminate. And Checked property value set to False, when CheckState property value is set to Unchecked.

When AutoCheck property is set to True, the CheckBox control automatically change the state depending on the user selection.

By using Appearance property we can change the control’s appearance as check box or button. When it set to Normal it shows as a check box, and when set to Button, it displays as a button.

Events

Most commonly used events are CheckChanged and CheckStateChanged events. These will be triggered when the state of the control is changed. 

CheckChanged event will be triggered when it’s Checked property value is changed.

CheckStateChanged event will be raised when it’s CheckState property value is changed.

(Raju)

One Comment

Add a Comment

Your email address will not be published.