Copying contents between Listboxes

I have two ListBoxes and wish to copy the contents of one, ListBox1:

to ListBox2. Both have the exact same variables and columns. I want to have the user double-click on a row to have this take place. How would I do this the easiest way?

lb2.AddRow()
lb2.Cell( lb2.LastIndex, -1 ) = lb1.Cell( row, -1 )

I should have specified the contents of the row that was double-clicked on I want to be copied from ListBox1 to Listbox2. Will the above work for this?

You can adapt the code as needed. The point is, specifying -1 as one of the parameters for Cell will grab the entire row or column. Specifying -1 for both parameters will grab the contents of the entire ListBox.

I tried this:

ListBox2.Cell(ListBox2.LastIndex, -1) = ListBox1.Cell(row, -1)

It says ‘row’ doesn’t exist. I tried ListIndex in its place, same error.

I’d also like to delete the row that was double-clicked on afterwards as well.

Sorry, it’s always hard for me to gauge the expertise of posters here.

“row” is just my stand-in variable. It’s meant to represent the row you are trying to copy from. Replace it with whatever value or variable that represents the row that you are trying to copy. If it’s the selected row, you can use ListBox1.ListIndex.

I’m very new to ListBoxes, and have found no code to do this sort of thing online. :slight_smile:

I tried this:

ListBox2.Cell(ListBox2.LastIndex, -1) = ListBox1.ListIndex

I get “Parameters are not compatible with this function” in the first section.

You could add this to the doubleclick event of listbox1

Listbox2.AddRow "" Listbox2.cell(Listbox2.LastIndex,-1)= me.cell(me.ListIndex,-1)

That works, Jim, thanks!

One thing that would be helpful to me too: How do I read each value in each cell sequentially in the row that is clicked into array variables? For example, the name of the person double-clicked on (the first cell in the row) i’d like to read into a variable called ydna(1), then the next cell (the person’s age) i’d like to read into a variable called ydage(1), and so on.

Hi Derek, do this when you fill your Listbox. Otherwise use nested for… next (code is not checked, I think ColumnCount is 1-based while ListCount is 0-based).

for x as integer = 0 to Listbox1.ListCount-1
 for y as integer = 0 to Listbox1.ColumnCount-1 

 YourArray(x,y) =Listbox1.Cell(x,y)
      
 next
next    
YourArray(x,y) = Listbox1.Cell(x,y)

So I change this to this, in the case of the first cell (person’s name)?

ydna(x,y) = Listbox1.Cell(x,y)

[quote=134123:@Derek DiBenedetto] For example, the name of the person double-clicked on (the first cell in the row) i’d like to read into a variable called ydna(1), then the next cell (the person’s age) i’d like to read into a variable called ydage(1), and so on.

[/quote]

I’m not sure that would be the best approach… you’ll end up with 2 arrays, each containing one value. You could pull the row into a single array using split like this

dim values() as string= Listbox1.cell(Listbox1.ListIndex,-1).Split(chr(9))

or you could read the cells individually and append them to arrays if you are looking to build a set of arrays for multiple rows.

Another solution to look at, if the goal is to build up a list, would be using an array of dictionaries.
You could even use listbox.rowtag to store the dictionary for each person.

I took the second approach and it worked fine, thanks Jim!