|
-
Mar 20th, 2014, 06:41 PM
#1
Thread Starter
Member
[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()
-
Mar 20th, 2014, 06:49 PM
#2
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
 
-
Mar 20th, 2014, 10:12 PM
#3
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.
-
Mar 21st, 2014, 09:00 AM
#4
Thread Starter
Member
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
-
Mar 21st, 2014, 09:03 AM
#5
Thread Starter
Member
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
-
Mar 21st, 2014, 12:00 PM
#6
Re: [RESOLVED] ListBox - selecting items is very slow
My usual boring signature: Nothing
 
-
Mar 23rd, 2014, 07:02 AM
#7
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|