Results 1 to 6 of 6

Thread: Determine ListView SelectedItems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2000
    Location
    Orlando, FL
    Posts
    253
    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.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2000
    Location
    Orlando, FL
    Posts
    253
    It is faster now that I am using a For...Each loop to iterate through the collection of listItems Thanks.
    Always looking for a better and faster way!

  4. #4
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    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.
    -Excalibur

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2000
    Location
    Orlando, FL
    Posts
    253

    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.
    Always looking for a better and faster way!

  6. #6
    Addicted Member
    Join Date
    Jan 2000
    Location
    Fresno, California, USA
    Posts
    195
    Why not just use the index?

    nLstIndx = Listview1.SelectedItem.Index

    Or just use the SelectedItem.Text to grab the listitems text etc..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width