Wednesday, April 15, 2009

Have a CheckedListBox only allow one selection at a time

At first I thought the SelectionMode.One property might be what I want but that doesn't seem to do it.  The block of code below seems to do it.

In the Handles clause add the CheckedListBox(s) that you want to only allow one selection for.

  Private Sub myList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged, CheckedListBox2.SelectedIndexChanged
    For index As Integer = 0 To sender.Items.Count - 1
      If index <> sender.SelectedIndex Then
        sender.setitemcheckstate(index, CheckState.Unchecked)
      End If
    Next
  End Sub


No comments: