Tech Tip: Utility Method to Highlight Rows in Array Based List Box
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: March 17, 2018
Below is a utility method to highlight rows in an array based list box.
This utility method, Util_Highlight_Rows, will search all rows in the list box passed into the method and highlight any row where the data is found in any of the rows. The utility search is not case sensative, and will search the columns in a "contains" manner, meaning that if the word or letter is in any row at all it will highlight the row. The utility will also scroll to the first highlighted row found.
The Util_Highlight_Rows method takes in five inputs, defined as follows:
$1 // Text, Name of list box $2 // Text, variable for search $3 // Longint, color for highlight $4 // Longint, color for no-highlight (row background color) $5 // Boolean, True to run highlights or False to clear highlights |
Method:
// Util_Highlight_Rows C_TEXT($1;$2;$findMe_t;$listBoxName_t) C_LONGINT($3;$4;$highlightColor_l;$clearColor_l) C_BOOLEAN($5;$search_b;$found_b) ARRAY TEXT($colNames_at;0) ARRAY TEXT($headerNames_at;0) ARRAY POINTER($colVars_ap;0) ARRAY POINTER($headerVars_ap;0) ARRAY BOOLEAN($colsVisible_ab;0) ARRAY POINTER($styles_ap;0) C_OBJECT($foundRows_o) If (Count parameters=5) $listBoxName_t:=$1 $findMe_t:=Uppercase($2) $findMe_t:=".*"+$findMe_t+".*" $highlightColor_l:=$3 $clearColor_l:=$4 $search_b:=$5 LISTBOX GET ARRAYS(*;$listBoxName_t;$colNames_at;$headerNames_at;$colVars_ap;\ $headerVars_ap;$colsVisible_ab;$styles_ap) For ($j;1;Size of array($colVars_ap)) For ($i;1;Size of array($colVars_ap{$j}->)) $found_b:=Match regex($findMe_t;Uppercase($colVars_ap{$j}->{$i});1) If (($found_b) & ($search_b)) // matched, highlight row LISTBOX SET ROW COLOR(*;$listBoxName_t;$i;$highlightColor_l;\ lk background color) OB SET($foundRows_o;String($i);String($i)) If ($first_l=0) $first_l:=$i End if Else // not matched, or not searching, set to white (default background color) If (Not(OB Is defined($foundRows_o;String($i)))) LISTBOX SET ROW COLOR(*;$listBoxName_t;$i;$clearColor_l;\ lk background color) End if End if End for End for If ($first_l=0) $first_l:=1 End if // scroll to first position found OBJECT SET SCROLL POSITION(*;$listBoxName_t;$first_l) End if |
To test the method, consider a simple form with a search box (text variable searchValue) and an array based list box.
The search box has only one event which is On After Keystroke, triggering the call of the method.
The method is called either to highligh rows (fifth input variable is True) or to clear all highlights (fifth input variable is False). Below is the sample method call, which is the object method for the search variable. Simply put, if the search value is anything other than an empty string ("") then run the utility method to highlight the rows, otherwise run the utility method to clear the highlights.
Case of : (Form event=On After Keystroke) searchValue:=Get edited text If (searchValue#"") Util_Highlight_Rows ("ListBox";searchValue;0x00ADD8E6;0x00FFFFFF;True) Else Util_Highlight_Rows ("ListBox";searchValue;0x00ADD8E6;0x00FFFFFF;False) End if End case |
This triggers so that as the user types in the search box the form will highlight the rows where the search term is found.
Search example one:
Search example two: