Exact Y of a listbox celltop when scrolled

Does anyone know how to get the exact Y coordinate in the KeyDown event of a ListBox (so that I can popup a custom DropDown at that cell? The passed in y argument works fine if the grid is not scrolled, but if it is scrolled, the DropDown is not placed in the correct place, always too low. I’ve tried all kinds of approaches from stuff I have read on the forum (and in the old forum) and in the docs. I am sure I am missing something small, but I can’t get the y coordinate to be correct when the grid is not showing row 0 (meaning it is scrolled down some). Thanks.

I suppose you pop your menu in CellClick ?

The top of the row is at :

(row-me.scrollposition)*me.rowheight+me.top

To have the bottom of the row, add me.rowheight

If you want to get the exact position of the cursor, use System.MouseY.

Thanks, Michel, but in this case it is kicked off by the KeyDown event, and doesn’t involved the Mouse. I think I finally figured it out from some work I had done in another ListBox subclass, which looks just about what you had posted, just switched around a bit:

MyListBox.Top + MyListBox.GetRelativeRow(MyListBox.ListIndex - MyListBox.ScrollPosition)

Where GetRelativeRow is:

Return (row * Me.RowHeight) + Me.HeaderHeight

Thanks again, you unselfishly help a lot of people on the forum.

Uggh… wanted to edit the previous post but won’t let me, ME is the same as MyListBox there. I think the ratio of times we need to legitimately edit a post and the times the community is protected from confusion or misinformation in an maliciously edited post is in the neighborhood of 10,000 to 1.

Watch out if you generalize that code… Listbox.HeaderHeight will return the same value regardless of if that instance has a header or not!!!

  • Karen

Thanks for the tip, Karen, it is very specific to ListBoxes that have headers.

Since Keydown does not offer row, I suppose you are using CellKeyDown.

It could work in keydown as well, but row would have to be the selected one.

No, it is a subclassed listbox that I always keep track of added on properties, RowCurrent and ColCurrent (to draw an Excel style cursor). So I have that information available anywhere at all times.

Alright. Good :slight_smile: