Delete only selected rows

Hello,

I have a listbox that I left at selection type 1, could someone tell me what code I can use to delete only the selected rows ?

Go downto zero so that removed rows don’t interfere with the for loop.

for i as integer=listbox1.listcount-1 downto 0 if listbox1.selected(i) then listbox1.removeRow(i) end if next

Tanks Again Tom !

You are the best.

Paulo Vargas

and please wghat code will be when i want to delete and save all data from listbox with button_clear & button_save all what is in listbox?
i mean i made loadbutton to load text file inside the listbox, so now i want to make clear and save as buttons,
thanks

[quote=62746:@Tom Iwaniec]Go downto zero so that removed rows don’t interfere with the for loop.

for i as integer=listbox1.listcount-1 downto 0 if listbox1.selected(i) then listbox1.removeRow(i) end if next[/quote]
Question for you: Is my method dangerous?
I came up with it on my own just poking about the listbox docs when I needed it. I’ve noticed it’s a little slow with many rows, does your method have any noticeable wait time?

while Listbox1.SelCount > 0 Listbox1.RemoveRow(Listbox1.ListIndex) wend

[quote=80634:@Tim Parnell].

while Listbox1.SelCount > 0 Listbox1.RemoveRow(Listbox1.ListIndex) wend[/quote]
As you are just removing everything with that code you are better off with:

Listbox1.DeleteAllRows

It’s not deleting everything. It deletes just the selected rows.
When SelCount is zero it stops working, so no other rows get deleted.

I stand corrected. I just looked at the docs and from what I can see you are safe. Though I have to wonder which is faster. The code you posted or ignoring SelCount and using:

while Listbox1.ListIndex >= 0 Listbox1.RemoveRow(Listbox1.ListIndex) wend

Time for an experiment.
edit: and the answer is close to identical using a million rows.

[quote=80634:@Tim Parnell]Question for you: Is my method dangerous?
I came up with it on my own just poking about the listbox docs when I needed it. I’ve noticed it’s a little slow with many rows, does your method have any noticeable wait time?

while Listbox1.SelCount > 0 Listbox1.RemoveRow(Listbox1.ListIndex) wend[/quote]
This wil remove something but not just the selected rows since it eve checks to see which rows are selected
It would probably work if you only have single selection enabled but with multi select it’s definitely not right

Use Toms & it works either way

[quote=80665:@Norman Palardy]This wil remove something but not just the selected rows since it eve checks to see which rows are selected
It would probably work if you only have single selection enabled but with multi select it’s definitely not right

Use Toms & it works either way[/quote]
I would agree Tom’s looks safer (now that I’ve had a chance to look and understand from your view)

Though this is how I got the idea:

From Listbox.ListIndex

So the while statement says “while there is a selection, delete the lowest selected row.”
This is why I love software development. There’s more than one way to do things, and still more to be discovered!

Well … I’ll be damned it does exactly as you suggested Tim :stuck_out_tongue:
I’d never have guessed writing it that way - ever