I need some advise/help from more experienced Xojo developers.
I understand that SelectionChanged event is triggered either by user explicitly selecting row in the WebListBox or via code such as:
AccountsListBox.SelectedRowIndex = -1
I often need to select row in the WebListBox (ex: AccountsListBox) via code but I would like to avoid triggering AccountsListBox.SelectionChanged event.
Here is an example of the code that selects relevant row in AccountsListBox:
AccountsListBox.SelectedRowIndex = -1
For j As Integer = 0 To AccountsListBox.RowCount - 1
Var vAccountId As New NumberField(True,0)
vAccountId.Set ReadAccountID(j)
If vAccountId = mCurrentAccountRow.account_id.getDouble Then
AccountsListBox.SelectedRowIndex = j
Exit For
End If
Next
I thought about adding property to this WebPage to use it with a test inside AccountsListBox.SelectionChanged event but I am not sure when to set/clear this property.
What would be the best way to “do nothing” inside the AccountsListBox.SelectionChanged event when the code is changing AccountsListBox.SelectedRowIndex?
I would create a Boolean property at the scope of the page, something like ChangeRowInCode. When your code is about to select the row, set ChangeRowInCode to True. Then at the top of the SelectionChanged event you do:
If ChangeRowInCode = True Then
ChangeRowInCode = False
Return
End If
If you find yourself doing this a lot, you could subclass the WebListBox and add a custom method to programmatically change the row and then add a RowChangedInCode parameter to the event.
Great, thanks, I was thinking about this but wasn’t exactly sure at which point the event is triggered. I shall add that I use WebListBox with DataSource a lot.
will not trigger the WebListBox.SelectionChanged event.
However setting SelectedRowIndex to any other number within the range will trigger the event.
So the flag that you were suggesting will only work when code sets SelectedRowIndex to anything but -1.
I don’t want to say that there is anything wrong with it, just noticed this and I think it is sort of inconsistent as I thought that setting SelectedRowIndex to any value will trigger the event.