Results 1 to 10 of 10

Thread: Listview ....very SLOW when adding items

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    Exclamation Listview ....very SLOW when adding items

    Hello,

    I´m adding maybe 2-500 items into a Listview and its very slow...
    Anyone know a resolutiion to this problem....

    My code is very plain just a For loop which adds items while the recordset is not EOF ... so i don´t really know what could be wrong ....

    Thanks in advance
    vanki

  2. #2
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Hi vanki

    How about popping a copy of your code here for us to take a look at.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  3. #3
    New Member
    Join Date
    Dec 2002
    Location
    Randolph, NJ
    Posts
    6
    The problem might be that every time you add an item, the listview redraws itself, which can take a while. Before the loop, call the listview's beginUpdate() method and after the loop call its EndUpdate() method.
    I FOUND YOU

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    Red face Code

    For Each oResult In oSearch.SearchResults
    System.Windows.Forms.Application.DoEvents()
    lstvResults.BeginUpdate()
    If bStop = True Then
    Exit For
    End If
    Dim itmX As New ListViewItem()
    Dim s As Integer
    If oCols.Item(0).Name = "Size" Then
    itmX.Text = CInt(oResult.Fields.Item(oCols.Item(0).Name).Value / 1024) & "KB"
    Else
    itmX.Text = CStr(oResult.Fields.Item(oCols.Item(0).Name).Value)
    End If

    itmX.Tag = oResult.DocumentID

    If oResult.Extension = ".msg" Then
    itmX.ImageIndex = 10
    ElseIf oResult.Extension = ".doc" Then
    itmX.ImageIndex = 8
    ElseIf oResult.Extension = ".xls" Then
    itmX.ImageIndex = 9
    ElseIf oResult.Extension = ".ppt" Then
    itmX.ImageIndex = 18
    ElseIf oResult.Extension = ".mdb" Then
    itmX.ImageIndex = 20
    ElseIf oResult.Extension = ".gif" Or oResult.Extension = ".jpg" Or oResult.Extension = ".tif" Or oResult.Extension = ".bmp" Then
    itmX.ImageIndex = 22
    ElseIf oResult.Extension = ".pdf" Then
    itmX.ImageIndex = 24
    Else
    itmX.ImageIndex = 13
    End If
    If oCols.Count() = 1 Or oCols.Count > 1 Then
    For s = 1 To oCols.Count - 1
    If oCols.Item(s).Name = "Size" Then
    itmX.SubItems.Add(CInt(oResult.Fields.Item(oCols.Item(s).Name).Value / 1024) & "KB")
    Else
    itmX.SubItems.Add(CStr(oResult.Fields.Item(oCols.Item(s).Name).Value))
    End If

    Next
    End If

    lstvResults.Items.Add(itmX)

    lstvResults.EndUpdate()
    prgrBar.PerformStep()

    counter = counter + 1

    Next

  5. #5
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    I agree with DSAINMON and I think the beginupdate and endupdate is good to check, but you have to keep it outside the loop, not inside as you are now. Now you are calling the methods for every round of the loop, saving nothing.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    BeginUpdate ....EndUpdate

    I tried the BeginUpdate and EndUpdate and put it ouside of the for loop but it makes no difference ....

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You should also remove the DoEvents which will slow it down a lot.
    VB Code:
    1. [b]lstvResults.BeginUpdate()[/b]
    2.  
    3. For Each oResult In oSearch.SearchResults
    4. [b]'System.Windows.Forms.Application.DoEvents()[/b]
    5. If bStop = True Then
    6. Exit For
    7. End If
    8. Dim itmX As New ListViewItem()
    9. Dim s As Integer
    10. If oCols.Item(0).Name = "Size" Then
    11. itmX.Text = CInt(oResult.Fields.Item(oCols.Item(0).Name).Value / 1024) & "KB"
    12. Else
    13. itmX.Text = CStr(oResult.Fields.Item(oCols.Item(0).Name).Value)
    14. End If
    15.  
    16. itmX.Tag = oResult.DocumentID
    17.  
    18. If oResult.Extension = ".msg" Then
    19. itmX.ImageIndex = 10
    20. ElseIf oResult.Extension = ".doc" Then
    21. itmX.ImageIndex = 8
    22. ElseIf oResult.Extension = ".xls" Then
    23. itmX.ImageIndex = 9
    24. ElseIf oResult.Extension = ".ppt" Then
    25. itmX.ImageIndex = 18
    26. ElseIf oResult.Extension = ".mdb" Then
    27. itmX.ImageIndex = 20
    28. ElseIf oResult.Extension = ".gif" Or oResult.Extension = ".jpg" Or oResult.Extension = ".tif" Or oResult.Extension = ".bmp" Then
    29. itmX.ImageIndex = 22
    30. ElseIf oResult.Extension = ".pdf" Then
    31. itmX.ImageIndex = 24
    32. Else
    33. itmX.ImageIndex = 13
    34. End If
    35. If oCols.Count() = 1 Or oCols.Count > 1 Then
    36. For s = 1 To oCols.Count - 1
    37. If oCols.Item(s).Name = "Size" Then
    38. itmX.SubItems.Add(CInt(oResult.Fields.Item(oCols.Item(s).Name).Value / 1024) & "KB")
    39. Else
    40. itmX.SubItems.Add(CStr(oResult.Fields.Item(oCols.Item(s).Name).Value))
    41. End If
    42.  
    43. Next
    44. End If
    45.  
    46. lstvResults.Items.Add(itmX)
    47. prgrBar.PerformStep()
    48.  
    49. counter = counter + 1
    50.  
    51. Next
    52. [b]lstvResults.EndUpdate()[/b]

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    need it :)

    I need the DoEvents so the user can stop the process

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think you may be stuck with it being slow then, especially if its 500 items. Or maybe only do events on every third item or every 5 items or something. See DoEvents has to check and let all other system processes run so it is checking on EVERY item and slowing you down.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    5

    well...

    I tried to comment out the DoEvents .... but the speed doesnt change at all

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