KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Highlight Listbox cell on mouse over
PRODUCT: 4D | VERSION: 14 R5 | PLATFORM: Win
Published On: January 30, 2015

The command OBJECT GET COORDINATES has the ability read Listbox parts in v14 R5. With this feature, it is possible to extract row and column information in a Listbox. The following method LISTBOX_HIGHTLIGHT_CELL takes advantage of this feature to hightlight a cell position with a mouse pointer in a Listbox.

// ----------------------------------------------------------------------
// Name: LISTBOX_HIGHLIGHT_CELL
// Description: Method will take the Column and Row position using the
// OBJECT GET COORDINATES to determine and highlight the cell position
// of the List Box.
//
// Parameters:
// $1 (POINTER) - Pointer Object for a List Box
// ----------------------------------------------------------------------
C_POINTER($1;$object;$columnPtr)
C_LONGINT(x;y;mbutton) // Mouse X, Y, and click state
C_LONGINT($i;$j;$k)
C_LONGINT($left;$top;$right;$bot;$rowNum;$rowHeight;\
  $headerHeight;$footerHeight;$hsHeight;$pos;$offset)
ARRAY TEXT(arrColNames;0) // List Box Column names

// Mouse
GET MOUSE(x;y;mbutton)

// List Box object
$object:=$1

// Extract the Column names
LISTBOX GET ARRAYS($object->;arrColNames;arrHeaderNames;arrColVars;\
arrHeaderVars;arrColsVisible;arrStyles;arrFooterNames;arrFooterVars)

// Finding the Row location
OBJECT GET COORDINATES($object->;$left;$top;$right;$bot)
$rowHeight:=LISTBOX Get rows height($object->;Listbox pixels)
$headerHeight:=LISTBOX Get information($object->;Listbox header height)
$footerHeight:=LISTBOX Get information($object->;Listbox footer height)
$hsHeight:=LISTBOX Get information($object->;Listbox hor scrollbar height)

If ((x>$left) & (x<$right) & (y>($top+$headerHeight)) & (y<($bot-$footerHeight-$hsHeight)))
   $pos:=LISTBOX Get information($object->;Listbox ver scrollbar position)
   $offset:=Mod($pos;$rowHeight)
   y:=y+$offset
   $rowNum:=Int(((y-$top-$headerHeight)/$rowHeight))+Int(($pos/$rowHeight))+1
Else
   $rowNum:=-1
End if

// Searching the boundaries of the Column location and highlight the cell
For ($i;1;Size of array(arrColNames))
   OBJECT GET COORDINATES(*;arrColNames{$i};$left;$top;$right;$bottom)


   If ((x>=$left) & (x<=$right))

      $columnPtr:=OBJECT Get pointer(Object named; arrColNames{1}) // Column pointer for size

       // Clear all the Rows
      For ($k;1;Size of array(arrColNames))
         For ($j;1;Size of array(Column1)) // Size of 1 column
            LISTBOX SET ROW COLOR(*;arrColNames{$k};$j;0x00FFFFFF;Listbox background color)
         End for
      End for

      // Highlight the particular cell
      LISTBOX SET ROW COLOR(*;arrColNames{$i};$rowNum;0x003875D7;Listbox background color)
      $i:=Size of array(arrColNames)+1
   End if
End for



Using the method in the object method of a Listbox where it will trigger the Mouse Move event:

Case of
   : (Form event=On Load)
   ARRAY TEXT(Column1;10)
   ARRAY TEXT(Column2;10)
   ARRAY TEXT(Column3;10)

   : (Form event=On Mouse Move)
   LISTBOX_HIGHLIGHT_CELL (->List Box)

End case



The Mouse Move event would need to be checked in the Listbox's property list shown below:



Examples of the mouse moving to different columns and highlighting a cell:






See Also: