Use of ThreeState property with CheckBox in VB.NET
A checkbox is clicked to select and clicked again to deselect some option. CheckBox controls have a function: they allow the user to choose from a list of options.
CheckBoxes are those controls which 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. CheckBox controls have a function: they allow the user to choose from a list of options. CheckBox controls let the user pick a combination of options. When a checkbox is selected a check (a tick mark) appears indicating a selection. The CheckBox control is based on the TextBoxBase class which is based on the Control class.
ThreeState property alternates between true and false with alternating clicks of the control. By default when the user uses the check box it only have two state and by using ThreeStates property you set it to true it behaves like a three state check box. The CheckAlign alternates between the MiddleRight and MiddleLeft values of System.Drawing.ContentAlignment and displays the values of three properties in a label.
Lets look an Example:
- First start an new windows application in VB.NET.
- By drag and drop or by coding create three checkbox and one label. Like below I create using code:
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.CheckBox2 =New System.Windows.Forms.CheckBox()
Me.CheckBox3 =New System.Windows.Forms.CheckBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
Me.CheckBox1.Location =New System.Drawing.Point(29, 37)
Me.CheckBox1.Name ="Box 1"
Me.CheckBox1.TabIndex = 0
Me.CheckBox1.Text ="Item 1 "
Me.CheckBox1.ThreeState =True
Me.CheckBox2.Location =New System.Drawing.Point(29, 77)
Me.CheckBox2.Name ="Box 2"
Me.CheckBox2.Size =New System.Drawing.Size(141, 29)
Me.CheckBox2.TabIndex = 1
Me.CheckBox2.Text ="Item 2 "
Me.CheckBox2.ThreeState =True
Me.CheckBox3.Location =New System.Drawing.Point(29, 117)
Me.CheckBox3.Name ="Box 3"
Me.CheckBox3.Size =New System.Drawing.Size(149, 29)
Me.CheckBox3.TabIndex = 2
Me.CheckBox3.Text ="Item 3"
Me.CheckBox3.ThreeState =True
Me.Label1.Location =New System.Drawing.Point(37, 181)
Me.Label1.Name ="Label"
Me.Label1.Size =New System.Drawing.Size(229, 61)
Me.Label1.TabIndex = 3
-
Now for the Label1 we use a property through which Label is stretch according to the text length:
Me.AutoScaleBaseSize = New System.Drawing.Size(10, 17)
Me.ClientSize = New System.Drawing.Size(297, 278)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1,Me.CheckBox3,
Me.CheckBox2,
Me.CheckBox1})
Me.Name = "Form1"
Me.ResumeLayout(False)
- Now under the checkbox click write this code:
Private Sub CheckBox1_Click(ByVal sender As Object,ByVal e AsSystem.EventArgs)Handles
CheckBox1.Click, CheckBox2.Click, CheckBox3.Click
Label1.Text = "List of selected checkboxs: "
If (CheckBox1.Checked) Then
Label1.Text = Label1.Text & " " & CheckBox1.Text
End If
If (CheckBox1.CheckState = CheckState.Indeterminate)Then
Label1.Text = Label1.Text & " " & CheckBox1.Text & " A "
End If
If (CheckBox2.Checked) Then
Label1.Text = Label1.Text & " " & CheckBox2.Text
End If
If (CheckBox2.CheckState = CheckState.Indeterminate)Then
Label1.Text = Label1.Text & " " & CheckBox2.Text & " A "
End If
If (CheckBox3.Checked) Then
Label1.Text = Label1.Text & " " & CheckBox3.Text
End If
If (CheckBox3.CheckState = CheckState.Indeterminate)Then
Label1.Text = Label1.Text & " " & CheckBox3.Text & " A "
End If
Label1.Refresh()
End Sub
- Now run the application the output window shows like below:
- After check all the checkbox's this window will show:
For the complete programme please download the source code from top of the article.
Happy Learning