10277 - Listbox.ColumnFromXY when cursor over header

I need to know when the mouse cursor is over a header cell or if it has just been clicked.
I though I could do it with “Column and Row FromXY”, and as I couldn’t do it I went to the Feedback just in case there was a reported bug about this subject. But what I found was a feature request from 2009 (#10277). The status is “verified”, but it seems that nothing has been done.

Of course, I can create my function to get the column of the header from XY, but I wanted to ask you all if you had already done it in an easier way.
Thanks for any help.

IIRC

If Y < me.headheight

Well, I’ve created this function and it works

Function HeaderColumn(extends LB as ListBox, X as Integer, Y as integer) As Integer
  
  'Returns the Header column according to X,Y
  'If X,Y is not on the Header it returns -1
  
  If LB = Nil Then Return -1
  If Y > LB.HeaderHeight Then Return -1
  
  Var LeftLB As Integer = LB.Window.Left + LB.Left
  Var Left As Integer
  
  For c As Integer = 0 To LB.LastColumnIndex
    Left = Left + LB.ColumnAt(c).WidthActual
    If Left > x Then Return c
  Next c
  
  Return -1
  
End Function

I don’t know if it works with ContainerControls, but fortunately it is not a problem now.

The followng in MouseDown of a standard ListBox:

System.DebugLog "ColumnFromXY:: " + Str(Me.ColumnFromXY(x,y))
System.DebugLog "RowFromXY:: " + Str(Me.RowFromXY(x,y))

Returns -1 (x and y) if click in the ListBox Header.

Exactly. This is the problem. I think that in the ListBox Header it should return the column number, not -1.

You still are able to determine that using System.MouseX and System.MouseY compared to ListBox location on the Window.

And the headerpressed event fires and gives you the column. What else do you need?

1 Like

Thank you Tim. You are right and, in fact, I was not aware of this event.
However my situation is a bit different. I was hovering the mouse over the header and I wanted to use the X,Y of the MouseMove event to know over which Header column I was. Without “pressing” it.

Yes, sorry, only later did I read the first post or two. There is unfortunately no MouseOverHeader AFAIK.

But you could do:

if  (Y<me.HeaderHeight)  Then
  yy = Y + me.HeaderHeight
  col = Me.ColumnFromXY(x,yy)
else
  col = Me.ColumnFromXY(x,y)
end if

:+1:

http://documentation.xojo.com/api/os/system.html#system-mousex

Or even:

col = if  (Y<me.HeaderHeight, Me.ColumnFromXY (x, me.HeaderHeight + 1), Me.ColumnFromXY (x, y))

That is basically what I have done in the past… but be aware that only works if the listbox is not empty to get the column.

If the listbox has no entries then col = -1 and one would need to use his original way…

As an aside, his original code does not account for the horizontal scroll position for the general case.

-Karen

1 Like