KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Save and restore the column position of all types of Data Sourced Listboxes
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: August 30, 2018

The ability to save and restore the column position of a Listbox can be done for all types of Data Sourced Listboxes with the following utility method:

// -----------------------------------------------------------------
// Name: SAVE_RESTORE_LB_COL_POSITION
// Description: Method will save and restore the Column position of all
// types of Listboxes.
//
// Input Parameters:
// $1 (TEXT) - Name of the Listbox
// ------------------------------------------------------------------
C_TEXT($1;$lbName)
C_COLLECTION($col)
C_TEXT($locJSON;$jsonText)

$locJSON:=Get 4D folder(Current resources folder)+"lbPrefs.json"

If (Count parameters=1)
  $lbName:=$1
  
  ARRAY TEXT($arrColNames;0)
  ARRAY TEXT($arrHeaderNames;0)
  ARRAY POINTER($arrColVars;0)
  ARRAY POINTER($arrHeaderVars;0)
  ARRAY BOOLEAN($arrColsVisible;0)
  ARRAY POINTER($arrStyles;0)
  
  LISTBOX GET ARRAYS(*;$lbName;$arrColNames;$arrHeaderNames;\
  $arrColVars;$arrHeaderVars;$arrColsVisible;$arrStyles)
  
  $col:=New collection
  
  For ($i;1;Size of array($arrColNames))
   $col.push($arrColNames{$i})
  End for
  
  $jsonText:=JSON Stringify($col)
  
  TEXT TO DOCUMENT($locJSON;$jsonText)
  
Else // Read back to order the Listbox column
  
  C_COLLECTION($textCol)
  
  If (Test path name($locJSON)=Is a document)
   $jsonText:=Document to text($locJSON)
   $textCol:=JSON Parse($jsonText;Is collection)
  
   For ($i;1;$textCol.length)
    LISTBOX MOVE COLUMN(*;$textCol[$i-1];$i)
   End for
  
  End if
  
End if


Here is an example of a Listbox with three columns:



An example here will move column "Name" to the left as shown:




This can be saved for example on the "On Close Box" form event:

Case of

  :(Form event=On close Box)

   SAVE_RESTORE_LB_COL_POSITION("List Box")

End Case



Then it can be restored back on the "On Load" event:

Case of

  :(Form event=On Load)

   SAVE_RESTORE_LB_COL_POSITION

  :(Form event=On Close Box)

   SAVE_RESTORE_LB_COL_POSITION("List Box")
   Cancel

End Case