Ask the Expert

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
            chkstate = CheckState.Checked
        End If
        ' Set the new state.
        CheckedListBox1.SetItemCheckState(sel, chkstate)
    End If
End Sub

This was first published in December 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: