Windows Develop Bookmark and Share   
 index > Windows Forms General > CheckedListViewItemCollection.Count does not work as expected
 

CheckedListViewItemCollection.Count does not work as expected

Could anyone let me know if there is something amiss in this code? I want to enable a menu option only if the user has checked on atleast one of the listview items. The items in listview are added at run time

Private Sub ListView1_ItemCheck(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck

Dim checkedItems As ListView.CheckedListViewItemCollection = ListView1.CheckedItems()

'MsgBox(checkedItems.Count)

If checkedItems.Count > 0 Then

SomeMenuItem.Enabled = True

Else

SomeMenuItem.Enabled = False

End If


End Sub


The code is returning count as zero when I check on an item in listview. However, on consecutive check it atleast returns some values

E.g. On the first check, the count returned is '0'. However, on the second check, the count returned is '1'

If I go and uncheck any one of the two items, then the count returned is '2' !

Can anyone please explain this behavior of "Count" in CheckedListViewItemCollection?

~Apploader
Apploader  Tuesday, September 15, 2009 1:57 PM
basically you implemented the ItemCheck event which is called before the item check state going to be change,
Put your code by implementing the ItemChecked event of the listview. the two events are different.
  • Marked As Answer byApploader Tuesday, September 15, 2009 3:09 PM
  •  
AUmidh  Tuesday, September 15, 2009 3:04 PM
basically you implemented the ItemCheck event which is called before the item check state going to be change,
Put your code by implementing the ItemChecked event of the listview. the two events are different.
  • Marked As Answer byApploader Tuesday, September 15, 2009 3:09 PM
  •  
AUmidh  Tuesday, September 15, 2009 3:04 PM

You can use google to search for other answers

Custom Search

More Threads

• .resx in c#
• Can't hide a tabpage
• How to Implement multiple Selection in a List Box
• ComboBox.SelectedIndex problem
• Tab control and Web Browser
• Problem Sorting Items in List View Control
• Using My Settings to set properties of controls
• enabling notify icon hidding
• How to switch using shortcuts between available resource files?
• VS 2005 IDE Crash