Question : Why does a checkedListBox fire the checkchanged when not clicking on the item

I am using VB.Net 2008 in a windows form.  I am using a CheckedListBox that has several items in it.  Several items are checked and not checked.   The strange part is when I have an item selected, but click on the lower part of the list where there are no rows, it is changing the state of the checkbox even when I am not anywhere near the checkbox!   How do I stop this from happening.  My users are going to freak out if they see the checkbox change when they click on the white area of CheckedListbox where there are no items.   The problem is this event is firing other logic that I do not want to happen unless they really mean to check the item.  I need to know when they really check on the box not on bogus white space in the box.   We could use HitTest on other controls, but HitTest is not an option on this control

By the way  CheckOnClick is set to false

It seems ridiculous that the ItemCheck and SelectedValueChanged events fire when clicking on the white space of a CheckedListbox...   I don't see any way to trap that an item was really checked/unchecked.  I don't want this event fired on the current selected item when I click on the white space under the items.  Sounds like a bug to me, but maybe I am missing something...  

I still need to be able to use the SelectedIndexChange event to know when an item is selected, but I also need to know when an actual checkbox in the control is checked/unchecked properly not by clicking on white space below the items.

Any help would be appreciated

Answer : Why does a checkedListBox fire the checkchanged when not clicking on the item

You can Inherit from the CheckedListBox and prevent the undesirable action like this:
(just hit "Build" and your new control should appear at the top of the ToolBox)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Public Class MyCheckedListBox
    Inherits CheckedListBox

    Private Const WM_LBUTTONDOWN As Integer = &H201
    Private Const WM_LBUTTONUP As Integer = &H202
    Private Const WM_LBUTTONDBLCLK As Integer = &H203

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK
                Dim pt As Point = Me.PointToClient(Cursor.Position)
                For i As Integer = Me.TopIndex To Me.Items.Count - 1
                    If Me.GetItemRectangle(i).Contains(pt) Then
                        MyBase.WndProc(m)
                        Exit Sub
                    End If
                Next
                Exit Sub ' Left click occurred in the white space: suppress default action

        End Select
        MyBase.WndProc(m)
    End Sub

End Class
Random Solutions  
 
programming4us programming4us