Results 1 to 7 of 7

Thread: [RESOLVED] ListBox - selecting items is very slow

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    38

    Resolved [RESOLVED] ListBox - selecting items is very slow

    Hi,
    I have a listbox which contains about 90 items, all short strings of about 5-6 characters.
    In my code, I have an array of strings, which I am iterating through, and each one that matches to an item in the listbox I am setting to 'selected'.
    The problem is, this process is taking way too long - for instance if I'm selecting 80 of the 90 items, it takes about 20 seconds.
    I have tried different methods of setting the selection - looping through the objects looking for the match, then setting it to true;
    using 'setSelected' based on location of match; using 'SelectedItems.Add' - they all take just as long to go through.
    I tried adding 'SuspendLayout with no difference, and several other things.

    The form on which the listbox resides has a large number of controls on it; I am guessing that maybe it's refreshing the screen between each selection - I seem to detect the screen flickering while I'm waiting for the code to finish.

    Please advise on what I can try to get this to run quicker.
    Thanks!

    Code:
    Me.lstPossibleReports.Select()
    Me.SuspendLayout()
    Me.lstPossibleReports.SelectedItems.Clear()
    For i = 0 To intSelectedRpts
       If arrAccountInfo(0, intindex) = arrSelectedRpts(0, i) Then 
    	   If lstPossibleReports.Items.Contains(arrSelectedRpts(1, i)) Then
    		   'lstPossibleReports.SetSelected(lstPossibleReports.Items.IndexOf(arrSelectedRpts(1, i)), True)
    		   lstPossibleReports.SelectedItems.Add(arrSelectedRpts(1, i))
    	   End If
       End If
    Next
    Me.ResumeLayout()

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,018

    Re: ListBox - selecting items is very slow

    Rather than Me.SuspendLayout, try listPossibleReports.SuspendLayout. The whole form won't refresh unless you tell it to, or unless you do something that requires it. The listbox itself will have to refresh a fair amount. You would want to use SuspendLayout on the listbox when filling it, though I'm not sure whether it will do anything good when all you are doing is selecting or scrolling.

    In fact, I don't believe the problem is in the code you posted. There are a few things that seem a bit inefficient, but not particularly bad.

    I would suggest putting a breakpoint early in that code, then pressing F11 to step forwards. What I am suspecting is that your actions are triggering other events, and it is those other events that are causing the slowdown rather than the code shown. Stepping through the code with F11 will show that in most cases.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,950

    Re: ListBox - selecting items is very slow

    You should probably be calling the ListBox's BeginUpdate and EndUpdate methods rather than any SuspendLayout and ResumeLayout methods. Also, rather than clearing all selections and then looping, I'd just loop like this:
    Code:
    For i = 0 To lstPossibleReports.Items.Count - 1
        lstPossibleReports.SetSelected(i, arrAccountInfo(0, intindex) = arrSelectedRpts(0, i) AndAlso lstPossibleReports.Items.Contains(arrSelectedRpts(1, i)))
    Next
    I'm not sure that that will help because, as Shaggy suggests, the issue is quite possibly elsewhere, but that's a better way to go regardless.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    38

    Re: ListBox - selecting items is very slow

    By jove you were right! I never thought to step into the 'SetSelected' line, but when I did, I discovered that it was firing the index changed event, which in turn was updating the visibility on all of the other controls on the form. That explains the flickering screen. Based on my research it seems that this only needs to be done once, so I added a switch so it would bypass the index changed code except for the last time.
    Problem Solved.
    Thanks!
    PD

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    38

    Re: ListBox - selecting items is very slow

    Hi, jmcilhinney,
    As you noted, it was just as Shaggy had suspected. But I thank you for the suggested improvements to my code. And yes, the Begin/EndUpdate methods are more appropriate here, it would seem. The layout methods are more related to control size, position, etc.
    Regards,
    LuckyJack

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,018

    Re: [RESOLVED] ListBox - selecting items is very slow

    Praise Jove.
    My usual boring signature: Nothing

  7. #7
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: [RESOLVED] ListBox - selecting items is very slow

    Awesome find, another way to help with that kind of thing is to set the forms doublebuffer to true. It is great when large amounts of processing or image work is needed (or combination of both). however the code being good and efficient from the start of course helps glad you found the issue
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

Tags for this Thread

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