listBox access

  • Thread starter Thread starter Michel
  • Start date Start date
M

Michel

Hi,
I've a listBox with 2 columns with the first set as hidden.
How can I consult the 2 columns of all the element of the listbox.
I thought about somethings like this
For i = 0 To listBox.ListCount - 1
Debug.Print listBox.ItemData(i).Column(0)
Debug.Print listBox.ItemData(i).Column(1)
Next i

but there is not Column for the ItemData. Is there a solution?

Thanks
Michel
 
Michel,

Try:

For i = 0 To listBox.ListCount - 1
Debug.Print listBox.Column(0, i)
Debug.Print listBox.Column(1, i)
Next i

Or,

For i = 0 To listBox.ListCount - 1
For j = 0 To listBox.ColumnCount -1
Debug.Print listBox.Column(j, i)
Next j
Next i

HTH,
Nikos
 
ItemData is usually used when looping through the ItemsSelected property of
a listbox. It will return the value from the Bound Column. If all you want
is the values from the columns, then use the Column property instead of the
ItemData property. The main advantage of the ItemData over Column is that
you don't need to know which column has been designated as the Bound Column.
You could use Column in lieu of ItemData to get the value of the Bound
Column, but you would have to know which column to specify. The Column
property accepts two indexes, the first one is the column index and the
second is the row index (both zero based). So, to get what you've indicated,
modify your code as follows:

For i = 0 To listBox.ListCount - 1
Debug.Print listBox.Column(0, i)
Debug.Print listBox.Column(1, i)
Next i
 
There is a column property for listboxes, but you also need to indicate the
row you're working with. Try this code...
'********code start**********
Dim i As Long, j As Long
Dim str As String
For i = 0 To listbox.ListCount - 1
For j = 0 To listbox.ColumnCount - 1
str = str & listbox.Column(j, i) & " "
Next j
Debug.Print str
str = ""
Next i
'***********code end**********
 
Back
Top