Copy ListBox1 to ListBox2

I know this code works:

listbox2.ColumnCount=listbox1.ColumnCount Dim i, j As Integer For i =0 To listbox1.ColumnCount listbox2.AddRow For j = 0 To 2 listbox2.Cell(listBox2.LastIndex,j) = listbox1.Cell(listBox2.LastIndex,j) Next j Next i

But is is possible to do this in one step?

I thought the ListBox.dataField would grab the all the data in one fell swoop, but that’s not the case.

If they both have the same number of columns you can use Cell(-1, -1) to get and set the entire listbox contents.

Thanks Tim! That worked!

listbox2.Cell(-1, -1) = listbox1.Cell(-1, -1)

What I’d really like to do is move the entire contents of a listbox into a string, (so I could save everything in one block), and then later load that string back, to restore the original populated state of the listBox

Considering how the listBox.initialValue is a text block, I thought maybe the listBox.dataField might work.

Listbox.Cell returns and accepts a string, so I would think this should work :slight_smile:

Got it. (These objects just have a maze of properties.)

This converts the entire listBox to Text, then loads the text back again.

Dim temp As String temp = listbox1.Cell(-1, -1).ToText //saves as text MsgBox(temp) //displays Text listbox1.Cell(-1, -1) = "" //clears text MsgBox ("Cleared") listbox1.Cell(-1, -1) = temp //loads text back MsgBox ("Back Again")

ToText is not required. In fact, it does a lot more work, as it takes a String (Cell(-1,-1)), converts it to a Text (ToText) and then auto-converts it back to a String to assign to Cell(-1,-1).

DeleteAllRows would be preferable to Cell(-1, -1) = “”.