ListBox Loop

I read the docs and got this base code from there.

For Each row As DesktopListBoxRow In ListBox1.Rows
  If row.Tag = "Taxable" Then ComputeTaxes(row.CellTextAt(0))
Next

Source for above: DesktopListBox — Xojo documentation Rows As Iterable

So I modified it a little bit and…

For Each row As DesktopListBoxRow In lstBxFinalList.Rows
  sOutput.AddRow lstBxFinalList.CellTextAt( lstBxFinalList.SelectedRowIndex, 1 ) + "," _
  +  lstBxFinalList.CellTextAt( lstBxFinalList.SelectedRowIndex, 2 )
Next

I am getting an out of bounds exception. I am building an output file to be written to disk for use elsewhere. sOutput is what will be used to write the file later on and it is an array.

The exception is coming from either the row or column parameter of the CellTextAt() methods. Rows and columns are zero-based so the last column or row would be Count - 1. lstBxFinalList.SelectedRowIndex would be -1 if there is no selected row, and the hard-coded column index of 2 could be too high (that is, you only have two columns, so the last column index is 1).

Not sure what you are trying to do, but usually when we use For Each row, we use ‘row’ in the code inside that For Each (as in the example). I don’t see you using ‘row’.

For Each row As DesktopListBoxRow In lstBxFinalList.Rows
  // We don't need to add a row for the following CellTextAdd's because our row is already "the row" ;-)
  sOutput.AddRow row.CellTextAt( 1 ) + "," + _
    row.CellTextAt( 2 )
Next
1 Like

Ah, so syntax error… and that worked! Hmm, thank you!