How to determine which of the visible rows clicked in a listbox?

I was planning to use a ‘popover’ (a fake one based on a container) that shows to the side of the listbox row to allow editing of data (the listbox row only has one cell and draws several lines of text in different styles to make it visually appealing but doesn’t allow inline editing). The popover was intended to align to the centre of the clicked row…

I had thought that i can can estimate where the pointy bit of the popover should align by using the local x, y in the cellClick event, the row parameter of the cellClick and row.defaultRowHeight… it didn’t seem complicated as the ‘pointy’ bit will always be in the centre of the fake popover: = ( + listbox.DefaultRowHeight/2 + row * listbox.DefaultRowHeight) - Popover.height/2

This worked perfectly for the first set of rows, but as soon as you scroll, the ‘row’ parameter of course will give a row number higher than the visible set of rows, so the popover container renders lower down and often outside the window.

So the question is, how can i determine which of the visible rows has been clicked?

If the listbox can show 5 rows and the user scrolls so that the 25th row is the 3rd one down on the visible rows, how can i detect that it’s the 3rd row down has been clicked?

I could keep track of the scrolling via the mousewheel event - or is there a more elegant solution?


P.S. It helps to look in the documentation from time to time - it is actually quite decent.

Hi @MarkusWinter - thanks but i think RowFromXY returns the actual row index, not the number of the visible row (in my previous example this returns the 25th rather than the 3rd row, but i’d need the latter) – and a quick test shows me that is indeed the case.

I guess i could subclass the listbox with a property in to keep track of the scrolling but that seems fragile/tedious… hoping someone’s got other suggestions?

probably need both RowFromXY and SrollPosition and a tiny bot of math :slight_smile:

Thanks @npalardy!

BTW the cellClick event already provides the row, rowFromXY doesn’t add incremental information as far as i can tell.
ScrollPosition on the other hand is pretty much what i was looking for… so @MarkusWinter had a point that i should pay more attention to the documentation lol :slight_smile:

yeah the row is the actual row based from row 0
scroll position is which row is top most of the visible rows
subtract scrollposition from row and that should be the visible row that was clicked