Hi all,
while I was watching news on the TV, I was also coding a Delete All Selected Rows method.
Here the final code:
[code] Dim ListCnt As Integer // Number of Rows in LB
Dim LoopIdx As Integer // Loop Indice
Dim DelCnt As Integer // Number of Deleted Rows
Dim SelCnt As Integer // Number of Selected Rows
// Get the number of Selected Rows
SelCnt = LB.SelCount
// Exit if no Row is Selected
If SelCnt < 1 Then Exit // This can be tested earlier
// Flag a change occured (Changes have to be saved)
If SelCnt > 0 Then
isChanged = True
End If
// Get the number of Rows in LB
ListCnt = LB.ListCount
// Loop thru the Rows from the last to the first and delete any selected row
For LoopIdx = ListCnt DownTo 0
// Delete the row if it is Selected
If LB.Selected(LoopIdx) Then
// Delete the selected Row
LB.RemoveRow LoopIdx
// Inc the number of Selected Rows
DelCnt = DelCnt + 1
End If
// Check if I Deleted all Selected Rows
If DelCnt = SelCnt Then Exit // No more Rows to Delete ? Exit !
// To avoid 1, Infinite Loop
If UserCancelled Then Exit
Next[/code]
I set it in the window that use it instead in a Module (so no passed parameter, more saved time).
Questions:
a. Is it possible to speed up the shared code ?
b. Am I right to place the code in the only place (a Window) that use it ?
(Instead of in a Method with the ListBox reference - LB - as parameter)
c. Am I right to use an If line to exit if I found all Selected Rows ?
Related code:
If DelCnt = SelCnt Then Exit
That line makes the code to exits instead of letting it go down until LoopIdx = 0
.
If the Selected Rows are not located at the beginning of the ListBox, I will avoid some loop iterations. This will make a gain of some seconds to far more, depending on the ListBox contents (hundred to thousand Rows) and the lowest Selected Row number.
Note: I used DownTo
, like in a method to delete files on disk, to run the loop.