Reorder listbox rows without drag / drop

I have a multi column listbox. I want to select a row with the mouse and then use push buttons (Up and Down) to move the selected row up or down the listbox resulting in a new order of the rows in the listbox. In my mind I want there to be a MOVE method available but that’s not be. Can anyone point me to a concise / slick way to do this? Thanks

if your table is simple strings (or even it is not)

[code]
Function SwapRow(lb as listbox,row1 as integer,row2 as integer)
dim s as string=lb.cell(row1,0)
lb.cell(row1,0)=lb.cell(row2,0)
lbcell(row2,0)=s
end function

[code]

then just create a simple moveup or movedown function the passes the appropriate index values (rows)

Thanks for the reply. However, I inserted your code into my test app and found that it only moved the first column of the listbox. As I stated in my original post I am trying to move the row in a multi column list box. Should your code work in a multi column listbox? Am I still missing something? Thanks again for the reply.

The code I posted was for illustration purpose only…

add a FOR/NEXT loop to replace the “0” in each statement to move over each column

You should do more than just copy and paste code out of the forum. You should read it, comprehend how it works, and adapt it to your specific usage. You might just copy something malicious one day.

Function SwapRow(lb as listbox,row1 as integer,row2 as integer)
for i=0 to lb.columncount-1
dim s as string=lb.cell(row1,i)
lb.cell(row1,i)=lb.cell(row2,i)
lbcell(row2,i)=s
next i
end function

OK, thanks Dave. That’s the way I was I was headed without another way being suggested. I was hoping that a whole row of columns could be manipulated at once instead of cell by cell. I did pick up a couple of techniques in your code I didn’t think of. I appreciate the time you took to respond.

You should be able to grab the whole row at a time with

dim s as string=lb.cell(row1,-1)

[quote=279099:@Scott Siegrist]You should be able to grab the whole row at a time with

dim s as string=lb.cell(row1,-1)

well I’ll be darned… that DOES work :slight_smile:

And you can grab an entire column at a time with

s = lb.cell(-1, column)

And the entire contents of the listbox (as a tab-delimited file) with

s = lb.cell(-1, -1)

[quote=279088:@Dave S] Function SwapRow(lb as listbox,row1 as integer,row2 as integer) for i=0 to lb.columncount-1 dim s as string=lb.cell(row1,i) lb.cell(row1,i)=lb.cell(row2,i) lbcell(row2,i)=s next i end function[/quote]

This HAVE to be in the LR as an example to achieve not obvious things.

It tooks me a while just to figure it is possible to do that !
Nota: I do not recall how I’ve done that …

Tim: I use that trick to do… exactly that. Once I realized the part I talk above.

For some unknow reason, I had a brain blockage on what is possible to do with a ListBox… Even online editing was far away from my mind :frowning:

…or copy all content from one listbox to another with lb1.Cell(-1,-1) = lb2.Cell(-1,-1)
:slight_smile:

Thanks Tim and Marco!! That the kind of thing I was looking for!! I’ll check it out myself but in case I screw it up…will the opposite work? Assignment of the string to the listbox row?

dim s as string=lb.cell(row1,-1)
lb.cell(row2,-1) = s

I appreciate you guys taking the time to post these tips / techniques that are not obvious in the documentation (at least to me).

EDIT
Yes the opposite works. So the meat of the code to move a row up or down a listbox is now basically 3 lines instead of multiple for…next loops. Thanks everyone who repsonded.

And… what about:

LB.AddRow Array("Red Sox", "Yankees", "Orioles", "Rays", "Blue Jays")

to pre-fill a Listbox’ Row ?

Or simply

LB.AddRow "Red Sox", "Yankees", "Orioles", "Rays", "Blue Jays"

[quote=279046:@Dave S]if your table is simple strings (or even it is not)

[code]
Function SwapRow(lb as listbox,row1 as integer,row2 as integer)
dim s as string=lb.cell(row1,0)
lb.cell(row1,0)=lb.cell(row2,0)
lbcell(row2,0)=s
end function

[code]

then just create a simple moveup or movedown function the passes the appropriate index values (rows)[/quote]

It only returns an error when I try to use this code.
I have made a copy of the code, selected my canvas (an arrow pointng up/down), add event handler and selected MouseUp where I paste the code

lbcell does not exists. LBCell exists.