Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with CheckedListBox not triggering SelectedIndexChanged event

Status
Not open for further replies.

SpankySpanky

Programmer
May 25, 2005
21
AU
Hello all

I am having problems with a CheckedListBox, I am trying to use the SelectedIndexChanged event to detect when the user toggles the check box. I have the CheckOnClick property set to True so that the check is toggled when anywhere on the selected line is clicked.

The problem is that when I click quickly multiple times the check toggles but I dont always get an event fired. It works fine if I click slowly but I cant guarantee what the user will do.

The problem can be replicated if you create a new project with a CheckedListBox and two labels, turn on CheckOnClick for the CLB and paste in the following code:

'==================================================
Dim a As Integer=0, b As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Add("TestItem")
End Sub

Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
a += 1
Label1.Text = CStr(a)
End Sub

Private Sub CheckedListBox1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckedListBox1.MouseUp
b += 1
Label2.Text = CStr(b)
End Sub
'==================================================


The two labels count how many SelectedIndexChanged and MouseUp events have occured. When you click the TestItem, the check box toggles normally, and the events both count up normally. However when you click fast, the checkbox still toggles but the SelectedIndexChanged event trips only on some of the checkbox changes. MouseUp always trips as expected.

This seems like a bug to me... but only because I cant see what else I did wrong. I thought it might have to do with doubleclicking because there appears to be only half the events triggered, but still why is the checkbox toggling? Surely the number of events should track the checkbox...

I dont know what to do....

Any ideas? Anybody?

 
I think you'll have to use the 'mouseup event' if you want to capture all the changes in the value irrespective of the speed at which the click events take place.

when the speed is high and the clicks are similar to double clicks and if the value changes from 'select' to 'unselect' to 'select', the 'SelectedIndexChanged' event is considering the change in value as only 1 and not 2.
 
Well yes, thats precisely what the example shows; which is why it seems like a bug.

What is odd is that the checkbox responds with every click but the event does not. It defeats the benefit of the CheckedListBox if you have to build a separate structure to hold the status of the checks and then cycle through them manually to see if anything is changed.

You cannot rely on the response of the SelectedIndexChanged event because the user can change the status of the checkbox without notification being passed to the underlying code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top