|
CheckBox
The CheckBox Web server control gives us an option to select, say, yes/no or
true/false. A checkbox is clicked to select and clicked again to deselect some option.
When a checkbox is selected, a check (a tick mark) appears indicating a selection.
The class hierarchy for this control is as follows:
Object
Control
WebControl
CheckBox
CheckBox Event
The default event of the CheckBox is the CheckedChanged event
which looks like this in code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles CheckBox1.CheckedChanged
'Implementation Omitted
End Sub
|
Notable Properties
Notable properties of the CheckBox are as follows:
Checked: Gets/Sets whether a checkbox displays a check
Text: Gets/Sets the text caption for the checkbox
TextAlign: Gets/Sets the alignment of the text caption
CheckBox Sample
Drag two CheckBoxes, a TextBox and a Button control on to the form. Set the Text
property for the CheckBoxes as Male and Female. The following code will display the
text of the Checkbox that is checked in the textbox when the button is clicked.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked Then
TextBox1.Text = "Your Gender" & " " & CheckBox1.Text
ElseIf CheckBox2.Checked Then
TextBox1.Text = "Your gender " & " " & CheckBox2.Text
End If
|
CheckBoxList
The CheckBoxList Web server control displays a number of checkboxes at once. This
control provides a multi selection check box group that can be dynamically generated
with data binding. It contains an Items collection with members that correspond to
individual items in the list. To determine which items are checked, we need to test
the Selected property of each item in the list. The class hierarchy for his control
is as follows:
Object
Control
WebControl
ListControl
CheckBoxList
Notable Properties
Notable properties of the CheckBoxList control are as follows:
Items: Gets/Sets the collection of items in the list
RepeatColumns: Gets/Sets the number of displayed columns
in the check box list
RepeatDirection: Gets/Sets the display direction of checkboxes
RepeatLayout: Gets/Sets the checkbox layout
TextAlign: Gets/Sets the check box's text alignment
CheckBoxList Sample
Drag a CheckBoxList, TextBox and a Button on to the form. Select the CheckBoxList
and add some items to it using the Items property. The following code will display
the text of the checkbox that is checked in the textbox when the button is clicked.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
TextBox1.Text = CheckboxList1.SelectedItem.Text
End Sub
|
Live Code Demo
CheckBox Sample
|