[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()
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.
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.
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
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
Re: [RESOLVED] ListBox - selecting items is very slow
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