Matty Brown
Matty Brown

Reputation: 442

Which event do I use to react to a user checking an item in a ListView control (CF 3.5)?

When trying to use the ItemCheck event of a ListView, I have found that the event fires before the item is checked, so running code that looks to see which items are checked doesn't work as expected, because the item that's just been checked still has its old Checked value.

Private Sub MyListView_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles MyListView.ItemCheck

There's an ItemChecked event in .NET Framework, but alas, this is missing in .NET Compact Framework. Is there some other way of executing code after a ListViewItem has been checked?

Upvotes: 0

Views: 259

Answers (2)

Matty Brown
Matty Brown

Reputation: 442

After trying a few things and looking around the hidden events like MouseUp which don't seem to fire, I came up with this solution:

Private Sub MyListView_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles MyListView.ItemCheck
  Me.MyListView.Items(e.Index).Checked = CType(e.NewValue, Boolean)
  Call DoStuffWithCheckedItems()
End Sub

It seems to work quite reliably. I'm basically just updating the Checked property myself.

Upvotes: 1

Sam Makin
Sam Makin

Reputation: 1556

You can use this event, as the arguments expose the items which was checked and its value through the ItemCheckEventArgs.NewValue and ItemCheckEventArgs.Index properties.

You should be able to use this information, with the other items in your listview to determine the current state of the object.

Upvotes: 2

Related Questions