Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
' Create a static variable which retains it's value between
' calls to this event. We'll use this to know when we're
' in the process of checking/unchecking boxes so as not to
' cause an infinite loop (as this event is called when "any"
' item is checked/unchecked, even through code), preventing the
' dreaded Stack Overflow message.
Static m_isUpdating As Boolean
' If not already updating the checked listbox, continue...
If Not m_isUpdating Then
' Flag us as updating the checked list to prevent recursion
m_isUpdating = True
' See if the item being checked/unchecked is the "All" item
If CheckedListBox1.SelectedItem.ToString() = "All" Then
' If it is, check/uncheck all the other items
Dim index As Integer
' Step through every item in the list
For index = 1 To CheckedListBox1.Items.Count - 1
' Set the checked status of the item
CheckedListBox1.SetItemChecked(index, (e.NewValue = CheckState.Checked))
Next
ElseIf e.NewValue = CheckState.Unchecked Then
' If this isn't the "All" item and we're unchecking something, then
' Uncheck the "All" checkbox
CheckedListBox1.SetItemChecked(CheckedListBox1.Items.IndexOf("All"), False)
End If
' Reset the flag to indicate we're no longer updating the
' checked listbox, so that it will execute the next time
' the user checks/unchecks an item.
m_isUpdating = False
End If
End Sub