One of the problems on a ListBox when selecting all rows with the
usual loop is that the ListBox Change event will fire for each row
that is being selected. So, if you have 2,000 rows in a ListBox, the
Change event fires 2,000 times! And if you have (like I had) code on
the Change event to check for selected rows and do some stuff, then
you can imagine the problems it caused!
I noticed that when using the "Select All" from the Edit menu to
select all rows in a ListBox, the Change event only fires once and the
selection of all rows is very fast!
The way I did to mimic the "Select All" Edit menu was by using a
window boolean property "mSelectingAll" and on the "Select All"
PushButton used:
Sub Action()
Dim lb As ListBox = ListBox1
Dim iMax As Integer = lb.ListCount - 1
If lb.SelCount = iMax + 1 Then Return
#pragma DisableBackgroundTasks
lb.Visible = False
mSelectingAll = True
lb.Selected(iMax) = False
For i As Integer = 0 To iMax
If i = iMax Then mSelectingAll = False
lb.Selected(i) = True
Next
lb.Visible = true
End Sub
Note: The "lb.Selected(iMax) = False" line is to guarantee the last
row is unselected, or the ListBox Change event would not fire if the
last row was selected.
On the ListBox Change event the code will only run if the
mSelectingAll property is false:
Sub Change()
If Not mSelectingAll Then
Dim iMax As Integer = Me.ListCount - 1
For i As Integer = 0 To iMax
If Me.Selected(i) Then
// Do stuff
End If
Next
End If
End Sub
And it seems is working fine. If you find any problem with this
solution, a way to improve it or other way to do it, please post.
I noticed there's a VERY OLD (since 2002) feature request for
ListBox.SelectAll with just 7 votes. Don't you need this feature
implemented? Report id: lvuwvvrk
http://snipurl.com/lvuwvvrk
Carlos
[2005r4/2006r2 - Win2K]
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>