Removing a row in a listbox

I’m a few weeks new to Xojo and I’ve hit some strange behaviour when trying to remove a row from a listbox. As a result I’ve been experimenting as follows:

If I code:

Card_List.RemoveRow(1)

then I correctly get the second row of Card_List removed, but if I select the row to be deleted and code:

Card_List.RemoveRow(Card_List.ListItem)

then I don’t get the row removed and the debugger goes into a loop and I’ve lost control of my app

So I changed my code to:

Row As Integer
Row = Card_List.ListItem // breakpoint 1
Card_List.RemoveRow(Row)
{more code} // breakpoint 2

At breakpoint 1 the value of Row was 1, which is correct.
On resuming I never reached breakpoint 2 so it was hanging on line 3.
Has anyone seen this? I get the impression that there’s a bug in the RemoveRow method. It only seems to accept an integer and not an integer variable.
I’m using 2015r2.2 on a PC. Also tried this on an iMac and got the same.

for selected row use

Card_List.RemoveRow(Card_List.ListIndex)

Sorry, a typo: I code ListIndex - otherwise it wouldn’t have compiled!

What I’m saying is:
Card_List.RemoveRow(Card_List.ListIndex)
doesn’t work even when the value of ListIndex is correctly 1, but
Card_List.RemoveRow(1)
does work.

try this

select row 1 and click ‘remove selected row’

does it work?

Start a new project. Add a listbox (listbox1) and a button
in the Listbox open event put

for i As Integer = 0 to 9 me.addrow "Row Number: " + str(i) Next
in the button action event put

if Listbox1.ListIndex <> -1 Then dim row As Integer = Listbox1.ListIndex Listbox1.RemoveRow row end if
Run the project, select any row in the LB and click the button
Now go figure out what is different in your original project.

[quote=194634:@Roger Clary]Start a new project. Add a listbox (listbox1) and a button
in the Listbox open event put

for i As Integer = 0 to 9 me.addrow "Row Number: " + str(i) Next
in the button action event put

if Listbox1.ListIndex <> -1 Then dim row As Integer = Listbox1.ListIndex Listbox1.RemoveRow row end if
Run the project, select any row in the LB and click the button
Now go figure out what is different in your original project.[/quote]

I am using R2015R1

it removes the selected row

I don’t see how it will help me as to why, when my ListIndex is 1, it doesn’t remove row 1. I’ll just keep investigating. Maybe I’ll try 2015R1. Thanks.

Were have you placed this code? Is it anywhere within the ListBox (any Event of ListBox) ?
If you have placed it into the change event of the ListBox f.e., i would expect weird behaviour.

BTW: If you plan to later remove multiple selected Rows at once, always count down from the ListCount-1 to the first Row (= Row 0). Because the Row Numbers below the removed Rows are changing when you delete Rows. :wink:

An Example

For X As Integer = Card_List.ListCount-1 DownTo 0 If Card_List.Selected(X) Then Card_List.Remove(X) End If Next X

The code’s in a button Action event - and I’m only deleting a single row, but thanks for the advice onl multiple rows.

I do have a ListBox Change event and wondering if that is the cause. I’ll try another event and see if that causes it. Thanks.

Yes, that was the cause. The ListBox change event was firing off some other code. All sorted. Thanks everyone.

[quote=194634:@Roger Clary]Start a new project. Add a listbox (listbox1) and a button
in the Listbox open event put

for i As Integer = 0 to 9 me.addrow "Row Number: " + str(i) Next
in the button action event put

if Listbox1.ListIndex <> -1 Then dim row As Integer = Listbox1.ListIndex Listbox1.RemoveRow row end if
Run the project, select any row in the LB and click the button
Now go figure out what is different in your original project.[/quote]
Roger,
How to add msg and action asking by Yes or No (to remove the selected row) ?

[quote=490732:@Djamel AIT AMRANE]Roger,
How to add msg and action asking by Yes or No (to remove the selected row) ?[/quote]

@Djamel AIT AMRANE

This should give you a basic Idea of how to do that Delete with Message

[quote=490761:@brian franco]@Djamel AIT AMRANE

This should give you a basic Idea of how to do that Delete with Message[/quote]
Thank you Brian,
i should change some features not present in my 2015r1.

[quote=490770:@Djamel AIT AMRANE]Thank you Brian,
i should change some features not present in my 2015r1.[/quote]
This one should work with 2015 API 1 Version

Thank you Brian,
perfectly !