Q

Unchecking items in a CheckedListBox

How do I uncheck items in a CheckedListBox?
Private Sub Form1_Load(ByVal sender As System.Object, _
                       ByVal e As System.EventArgs) _
                       Handles MyBase.Load
    CheckedListBox1.Items.Add("First", True)
    CheckedListBox1.Items.Add("Second", True)
    CheckedListBox1.Items.Add("Third", True)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button3.Click
    ' Get the selected item index.
    Dim sel As Integer = CheckedListBox1.SelectedIndex
    If (sel <> -1) Then
        ' Get the selected item's check state.
        Dim chkstate As CheckState
        chkstate = CheckedListBox1.GetItemCheckState(sel)
        ' Toggle the item state.
        If (chkstate = CheckState.Checked) Then
            chkstate = CheckState.Unchecked
        Else
            chkstate = CheckState.Checked
        End If
        ' Set the new state.
        CheckedListBox1.SetItemCheckState(sel, chkstate)
    End If
End Sub

This was first published in December 2003

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close