Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
InvalidArgument=Value of '10' is not valid for 'index'.
Parameter name: index



Please help me when i count how many record are checked or not with this code show error

InvalidArgument=Value of '10' is not valid for 'index'. Parameter name: index

https://ibb.co/eU6Wn9[^]


https://ibb.co/izkD0U

What I have tried:

Dim count As Integer
       For k = 0 To ListView1.Items.Count
           If ListView1.Items(k).Checked = True Then
               count = count + 1
           End If
       Next k
       Label2.Text = count
Posted
Updated 10-Sep-18 10:46am

Assuming that it's a WinForm project... A ListView object has got CheckedItems Property (System.Windows.Forms) | Microsoft Docs[^]
So, you don't need to loop through the collection of items in ListView.
VB.NET
Dim countOfcheckedItems = ListView1.CheckedItems.Count()
Label2.Text = countOfcheckedItems
 
Share this answer
 
The index is zero based, so you want to iterate to one less than the number of items

For k = 0 To ListView1.Items.Count-1
 
Share this answer
 
Comments
Maciej Los 10-Sep-18 16:40pm    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900