Setting selection in listbox after removing a row

I’m having a senior moment: how to I set the selection in the listbox and keep the selection color? My code is very simple:

dim oldListIndex as Integer = LBImapAccounts.ListIndex LBImapAccounts.RemoveRow(oldListIndex) LBImapAccounts.ListIndex = oldListIndex LBImapAccounts.Refresh RaiseEvent ImapAccountSelectionChanged LBImapAccounts.Cell(LBImapAccounts.ListIndex, 2)

The new row of the listbox is selected. But even with the refresh I don’t see the row selection color from the CellBackgroundPaint:

[code] dim col as color
if me.Selected(row) then
col = SelectedRowColor
globals.theErrorLog.LogItem CurrentMethodName
else
'draw alternativing background

end if
g.foreColor = col
g.fillrect 0, 0, g.width, g.height

return true[/code]

Does anyone have an idea why the SelectedRowColor isn’t shown?

Xojo 2015r1, Mac OS 10.10.

the refresh is not needed.
and in a small example here it works as it should.

I don’t know why Selected(row) doesn’t work, but I would try this first anyway:

[quote=171192:@Beatrix Willius]dim col as color
if row = me.ListIndex then
col = …[/quote]
Another thing: you will need to account for the case where the last row is removed or you will have an OutOfBoundsException on this line:[quote=171192:@Beatrix Willius]LBImapAccounts.ListIndex = oldListIndex[/quote]

Selected would work for multiple selection and works also fine with one selected row.

Perhaps an invalidateCell is in order

for col as integer=0 to LBImapAccounts.ColumnCount-1
    LBImapAccounts.invalidateCell(LBImapAccounts.ListIndex,col)
next

New day, brain working better. Of course, my code won’t work for the last row in the listbox because the listcount goes down. I only had to add

if LBImapAccounts.ListIndex = -1 then LBImapAccounts.ListIndex = oldListIndex - 1

after removing the row and now everything is working as it should.