-
Does anyone know a speedy way of determining the selected items in a ListView Control? I have a ListView that is populated with more than 6000 items and it takes too long to iterate through the entire list. There must be a faster way.
Code:
Dim intItem As Integer
Dim intCount as Integer
intCount = vewItems.ListItems.Count
For intItem = intCount To 0 Step -1
If vewItems.Listitems(intItem).Selected Then
vewItems.ListItems.Remove (intItem)
End If
Next
This works but is very slow. Still working on it.
-
Maybe you can send some messages to it:
Code:
Public Const LVM_GETSELECTEDCOUNT = (LVM_FIRST + 50)
Public Const LVM_GETSELECTIONMARK = (LVM_FIRST + 66)
Maybe when you have the selectedcount you could send a message with FindItem
Code:
Public Const LVM_FINDITEMA = (LVM_FIRST + 13)
Public Const LVM_FINDITEMW = (LVM_FIRST + 83)
Not sure though, look at http://www.mvps.org/vbnet/
for a lot of ListView stuff :)
-
It is faster now that I am using a For...Each loop to iterate through the collection of listItems Thanks.
-
You could also use the ListView.Tag ... As someone clicks in the ListView, check to see if the Item is selected or not during the click event. If the item is selected, place the item's index number and a comma in the tag (don't use spaces). You can check to see if it's in the tag by doing an instr function:
Code:
If InStr(1, ListView1.Tag, "," & ListView1.SelectedItem.Index & ",") Then
ListView1.Tag = Replace(ListView1.Tag, "," & ListView1.SelectedItem.Index & ",", ",")
Else
ListView1.Tag = ListView1.Tag & "," & ListView1.SelectedItem.Index & ","
End If
Now, granted, this might not work if 6000 items are all selected, but there is a good chance you can keep track of all the selected items without any iteration whatsoever.
Hope this helps.
-
Fast Code
This code is fast. It performed a procedure on 6000 items in 1.2 seconds.
This might be the best way to find the selected items.
Code:
Dim lstCount As ListItems
Dim lstItem As ListItem
Set lstCount = frmMain.vewItems.ListItems
For Each lstItem In lstCount
If lstItem.Selected Then
'Procedure
End If
Next
Thanks.
-
Why not just use the index?
nLstIndx = Listview1.SelectedItem.Index
Or just use the SelectedItem.Text to grab the listitems text etc..