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

jdressing
jdressing used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
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)

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

Open in new window

Author

Commented:
Perfect!  This solved my problem!  Thank you very much!!  I wonder why Microsoft doesn't fix this bug in their control... kind of weird why they would allow that behavior.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial