CheckBox lets user choose from a list of options by checking or un-checking them. User can select either single or multiple options from the list. The C1CheckBox class provides a Boolean property, Checked, to get or set the value of a checkbox with two states. You can also create a three-state checkbox with three states as Checked, Unchecked and Indeterminate by setting the ThreeState property to true. In such case, you can get the checked state using CheckState property which returns the current state of a checkbox.
Default CheckBox | Toggle Button (Appearance = Button) | Three-state CheckBox (ThreeState = true) |
---|---|---|
Create CheckBox control |
Copy Code
|
---|---|
chk = new C1CheckBox(); Controls.Add(chk); chk.TextImageRelation = TextImageRelation.ImageAboveText; chk.ThreeState = true; chk.Text = "Enable"; chk.Value = true; chk.Location = new Point(10, 50); |
You can display a checkbox in the form of a toggle button as well by setting the Appearance property to Button. The C1CheckBox also lets you set an Image on the control. You can set the TextAlign and ImageAlign properties to position the text and image respectively. You can also set the TextImageRelation property to specify the position of image in respect to the text. You can also choose to set icon in the control using the Icon property which supports bitmap, font and vector icons.
Binding a control with a data source gives you ability to communicate and even update the underlying data through the control. The CheckBox control can be bound to Boolean, String, and Integer fields of a data source using DataSource property. You can specify the field to be bound to CheckBox using the DataMember property.
Data Binding in CheckBox Control |
Copy Code
|
---|---|
c1CheckBox1.DataSource = bSource;
c1CheckBox1.DataMember = "Discontinued";
|
By default, DataType property of checkbox value is set to Boolean type. However, if you want to bind checkbox to a string or an integer type field, you can set the DataType property to string or int and then use the TranslateValues property to convert the boolean value of checkbox into a string or an integer.