Listbox to Array?

Hi team!

I hace a listbox with 4 columns and 7 rows.

I walk through the Listbox as this way and It works:

For i as integer=0 to listbox.listcount -1 'Cuenta las listas

For j as integer=0 to listbox.ColumnCount -1 'Cuenta las columnas
  redim my2Darray(i, j)
  Msgbox listbox.Cell(i, j)
  my2dArray(i,-1) = listbox.Cell(i,j)


I’m using Redim “my2Darray” to redimension my array to according of cells existing on the Listbox.
But how can I pass it to my Array Dim my2Darray(-1,-1) As String.

In order to assign the columns and rows?

Thanks in advance :stuck_out_tongue:

my2dArray(i,j) = listbox.Cell(i,j) Maybe?

By redimming your array each time thru the loop, you’re destroying what was previously there.

dim rowCt As Integer = listbox1.Listcount-1
dim colCt As Integer = listbox1.ColumnCount-1
redim my2Darray(rowCt, colCt
For i as integer=0 to rowCt
For j As integer = 0 to colCt
my2dArray(i, j) = listbox1.cell(i, j)

Untested code, but you get the idea

[code]Dim my2DArray(-1, -1) As String

ReDim my2DArray(Listbox1.ListCount - 1, Listbox1.ColumnCount - 1)

For i As Integer = 0 To Listbox1.ListCount - 1
For j As Integer = 0 To Listbox1.ColumnCount - 1
my2DArray(i, j) = Listbox1.Cell(i, j)

Only if the new dimensions are lower than the previous ones. In his code, they are not. Redim does not affect what is already within bounds.

(i,-1) ???

I think that is your problem. (As Gerardo already pointed out.)

Thanks to all!

In fact is my2dArray(i,j) = listbox.Cell(i,j)

But when I put redim inside the loop, When I try to getting value of the array I get no value.
So, when I redim the array outside the loop, It works. I can get any value of the array.

My apologies to Roger, you are correct. The J loop begins at zero each time, which wipes out the previous entries. I didn’t read closely enough.