Move a listbox row to the visible centre

Hi
I have a desktop listbox. The user can repeatedly select rows for updates. To make things easier if the visible bottom row is selected the listbox will automatically scroll so that the row is in the middle of the display. The Xojo behaviour seems odd, or maybe it’s just me.

// The selected row is the bottom of the window, move it up.
var noRows as Integer = (me.Height - me.HeaderHeight) \ me.RowHeight
var i as Integer = me.ScrollPosition + (noRows - 1) mod me.SelectedRowIndex
var i1 as Integer = me.ScrollPosition + (noRows - 1)

if me.ScrollPosition + (noRows - 1) mod me.SelectedRowIndex = 0 then
  me.ScrollPosition = me.ScrollPosition + (noRows \ 2) + 2
end if

This is the code I’m using (doesn’t really work, but stay with me).
The first VAR statement is calculating the number of visible rows in the listbox. The other 2 are for debugging.
In this case noRows evaluates to 9, therefore SelectedRowIndex is 8, As you can see the second VAR is the calculation to identify the bottom row. For the first page this code works. However if I remove the braces around (noRows - 1) it doesn’t work. It still evaluates to 8 but the IF statement comes out false and resetting scroll position doesn’t run. With the braces that works.
On the second page, noRows is still 9, SelectedRowIndex is 14, the second VAR sets i to 14 but the IF statement fails.
Can anyone explain what is happening here, or where my logic is wrong, and if that is the case how do I do what I want.

Exasperated.
Thanks

After I turned my computer off last night two thoughts occurred to me.
The first is that I am overcomplicating my basic requirement, the formula I needed was

If me.scrollPosition + noRows = me.selectedRowIndex then ....

May need to tweak that a little since ScrollPosition and SelectedRowIndex are 0 based and noRows is not. However, why let simplicity get in the way when you can try and be clever … :face_with_spiral_eyes: ??? :blush:

That doesn’t answer my basic question, why doesn’t my original solution work? According to the rules of Standard Precedence it should and the debugger bears that out. The expected values are correct. So why does the presence or otherwise of the brackets change the outcome in some occasions?

Which brings me to my second thought. All the values in the calculations are integers but of unpredictable value, so is the compiler converting them to doubles for the calculation and as we know comparing doubles is a bit hit and miss. This could explain why the execution flow is not what the debugger would imply, but still doesn’t explain why the brackets impact the outcome, but they do.

Jack

Why would it do that? Far as the compiler is concerened, ALL integers contain unpredictable values.

Can you create a sample project? Share with dropbox, one drive, google drive…

The MOD statement in my original code is checking the remainder, the remainder may involve a decimal point which integers do not support, so the output from MOD will involve a double.

https://www.dropbox.com/scl/fo/3f4t9ui7plrml9f0iot7q/AHb_ECrd7vNdb6F7e7sd4hs?rlkey=fx2aozouvqi4mi4gayjwuzmf4&dl=0

Done.
Follow the Dropbox link and you’ll find an app testListbox1.
When you run it, it will display a window (Window1) which has a listbox, select the bottom visible row and it will open a second window (Window2). This has a single button, press it and Window2 will close and it will return to Window1.
Go to the SelectionChanged event on the listbox. It has the brackets around noRows - 1.

var noRows as Integer = (me.Height - me.HeaderHeight) \ me.RowHeight
var i as Integer = me.ScrollPosition + (noRows - 1) mod me.SelectedRowIndex
var i1 as Integer = me.ScrollPosition + (noRows - 1)

if me.ScrollPosition + (noRows - 1) mod me.SelectedRowIndex = 0 then
  me.ScrollPosition = me.ScrollPosition + (noRows \ 2) + 2
end if

Remove the brackets and run it again. On this occasion the IF statement fails, whereas with the brackets it works. Var i and Var i1 are there purely for debugging.