KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to retrieve the listbox row the mouse cursor is currently over
PRODUCT: 4D | VERSION: 14.1 | PLATFORM: Mac & Win
Published On: September 3, 2014

Below is a utility method to retrieve the current row in a listbox that the mouse cursor is currently hovering over. This can be useful for simulating an On Mouse Over event in the listbox.

// Method: LISTBOX_GET_MOUSED_OVER_ROW
// Parameters
// $1 - pointer to the listbox object
//
// Return
// $0 - current row the mouse is over or -1 if mouse cursor is not over the listbox
// ------------------------------------------------------------------------------

C_POINTER($1;$LB)
C_LONGINT($0;$rowNum)
C_LONGINT($mX;$mY;$left;$top;$right;$bot;$mB;$rowHeight;$headerHeight;$pos;$offset)

If(Count Parameters=1)
  $LB:=$1
  OBJECT GET COORDINATES($LB->;$left;$top;$right;$bot)
  $rowHeight:=LISTBOX Get rows height($LB->)
  $headerHeight:=LISTBOX Get Information($LB->;Listbox header height)
  $footerHeight:=LISTBOX Get information($LB->;Listbox footer height)
  $hsHeight:=LISTBOX Get information($LB->;Listbox hor scrollbar height)
  GET MOUSE($mX;$mY;$mB)
  If (($mX>$left) & ($mX<$right) & ($mY>($top+$headerHeight)) & ($mY<($bot-$footerHeight-$hsHeight)))
    $pos:=LISTBOX Get information($LB->;Listbox ver scrollbar position)
    $offset:=Mod($pos;$rowHeight)
    $mY:=$mY+$offset
    $rowNum:=Int((($mY-$top-$headerHeight)/$rowHeight))+Int(($pos/$rowHeight))+1
  Else
    $rowNum:=-1
  End if
  $0:=$rowNum
End if


LISTBOX_GET_MOUSED_OVER_ROW method can be called during the form events On Mouse Move, On Timer, On Drop, On Drag Over, etc...

Case of
  : (Form event=On Mouse Move)
   rowNum:=LISTBOX_GET_MOUSED_OVER_ROW (->LB)
  : (Form event=On Timer)
   rowNum:=LISTBOX_GET_MOUSED_OVER_ROW (->LB)
end case