Listbox Cell x,y

I think I’m overlooking something but is there a way to find out the current x,y Left & Top of a given Listbox Cell?

I’m trying to EmbedWithin PopUpMenu’s but can’t seem to figure out how to position them.

Something like that works. I did not optimize it. I start X from zero, but it could start from the width of the preceding columns. Also, I placed a check for ScrollPosition, but if the listbox scrolls horizontally, you want also to check if the cell is visible. Unless you click on it, of course, then you can use a similar method to find the limits of the cell from the current mouse coordinates.

Function CellPos(extends LB as ListBox, Row as Integer, Column as Integer) As Integer() Dim Result(1), X, Y as Integer If Row < LB.ScrollPosition then Result(0) = -1 Result(1) = -1 return Result else Y = 1 For X = 0 to LB.width system.DebugLog str(LB.ColumnFromXY(X,Y)) if LB.ColumnFromXY(X,Y) = Column then Result(0) = X exit end if next For Y = 0 to LB.Height if LB.RowFromXY(X,Y) = Row then Result(1) = Y exit end if next end if Return Result End Function

Button :

Sub Action() Dim Result() as Integer = Listbox1.CellPos(3,1) MsgBox str(result(0))+" "+str(result(1)) End Sub

Thank you Michel!

Wouldn’t it be easier to directly calculate instead of searching

[code]Function CellPos(extends lb As Listbox, row As integer, column As integer) As REALbasic.Point

dim x As integer = -lb.ScrollPositionX
for i As integer = 0 to column - 1
x = x + lb.Column(i).WidthActual
next

dim y As integer = (row - lb.ScrollPosition) * lb.RowHeight

return new REALbasic.Point(x, y)

End Function[/code]

Nice!!
And the first time I see REALBasic.Point.
So cool. Learning every day. :slight_smile:
Thanks!

Will’s method won’t work if the listbox has headers. In that case, use Michel’s version.

Add this line second to last to account for a header

if lb.HasHeading then y = y + lb.HeaderHeight

Will’s method will work fine since vertically the ListBox is not using smooth scrolling, and ScrollPositionX is in pixels.

When did they add HeaderHeight?

Long time ago… Unfortunately it only it always return what teh header high SHOULD be IF it is turned on… If you are going to use it for any calculations you need to check if teh header is actually turned on.

If the header is turned off IMO it should return 0, but that is just me! :wink:

  • karen

HiTim:
do not worry, I too sometimes discover instructions added… many years ago…