OutOfBounds Exception on DesktopListbox

Hi All,

I am getting an out of bounds exception on a ListBox while getting the RowTagAt in the SelectionChanged Event.

I have two listboxes: Parent and Child. When I select a row in the parent I remove all the rows in the child and repopulate it with the appropriate child records.
Here is the code in the ParentListBox.SelectionChanged Event:

Var rs As RowSet

lbChild.RemoveAllRows()

rs = childrenByParent(lbParentList.RowTagAt(lbParentList.SelectedRowIndex).IntegerValue)
If rs <> Nil Then
  for each row as DatabaseRow in rs
    lbChild.AddRow row.Column("child_nm").StringValue
    lbChild.RowTagAt(lbChild.LastAddedRowIndex)= row.Column("child_id").IntegerValue
  Next
End If

In the Child Listbox the problematic code in the SelectionChange event is:

SelectedChiId = lbChild.RowTagAt(lbChild.SelectedRowIndex) <=OutOfBoundsException
MessageBox(str(SelectedChiId))

This works fine the first time I select a Parent and then Child record.
But when I select a new Parent / Child combination that is when I get the error.
Any ideas where I am going awry?

Thanks,

Craig

ListBox.RemoveAllRows will fire SelectionChanged, and SelectedRowIndex will be -1 at that time.

2 Likes

Hi Thom,

That is…interesting.
So should I wrap my code in a check to make sure the SelectedRowIndex > -1 ?

Craig

Absolutely. You cannot get a row tag for row -1.

1 Like

You should always wrap any SelectionChanged code in an IF condition. It fires more often than you expect.

Thanks for the advice. I am learning things have changed a wee bit in my absence. Any other hints or advice I am 100% OK with. :grinning:

Just for clarity, this -1 thing has always been true. In the old framework, ListIndex would also be -1 if there was no selection and you would also get OutOfBoundsExceptions when trying to access rows or rowtags.

1 Like

u can use DesktopListBox.NoSelection
or if you have any values for if then conditions better use a enumeration, not hard coded numbers.

Thanks @Greg_O and @MarkusR

@MarkusR I am using the RowTag to store the Primary Key for the record that is being selected so enumerations are not optimal for me in this situation, but I will absolutely keep that in mind!

Thanks!

No, DesktopListbox.NoSelection is a constant for -1.

1 Like

it is something like that instead of -1

if lbParentList.SelectedRowIndex = DesktopListbox.NoSelection then
else
end if

@MarkusR I see what you mean now.

Thanks!!