KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to resize list box columns proportionally
PRODUCT: 4D | VERSION: 13.1 | PLATFORM: Mac & Win
Published On: October 13, 2012

When resizing list boxes, only the last column grows. This Technical Tip describes a way to have all columns of a list box grow proportionally.

Here is a method that resizes the columns of a list box:

// ----------------------------------------------------
// Method: LISTBOX_RESIZE_COLUMN
// Description
// Resize all columns in a list box proportionally
// Parameters
// $1 (Pointer) - Pointer to a list box variable
// $2 (Pointer) - Pointer to array tracking the width of columns in the list box
// $3 (Pointer) - Pointer to longint variable tracking the width of the list box
// ----------------------------------------------------

C_POINTER($1;$listBox_p)
C_POINTER($2;$colWidthArr_p)
C_POINTER($3;$LB_width_l)
C_LONGINT($numColumns_l;$newColumnWidth;$i)
C_LONGINT($left;$top;$right;$bottom)

If (Count parameters>=3)

   $listBox_p:=$1
   $colWidthArr_p:=$2
   $LB_width_l:=$3

   $numColumns_l:=LISTBOX Get number of columns($listBox_p->)
   LISTBOX GET ARRAYS($listBox_p->;arrColNames;arrHeaderNames;arrColVars;\
      arrHeaderVars;arrColVisible;arrStyles)

   OBJECT GET COORDINATES($listBox_p->;$left;$top;$right;$bottom)
   $LB_newWidth:=$right-$left

   For ($i;1;$numColumns_l)
      $colWidthArr_p->{$i}:=Round($colWidthArr_p->{$i}*\
         ($LB_newWidth/$LB_width_l->);0)
      LISTBOX SET COLUMN WIDTH(arrColVars{$i}->;$colWidthArr_p->{$i})
   End for
   $LB_width_l->:=$LB_newWidth

End if


Since the columns of a list box can be resized and moved, here is a method to track the width of the columns given a pointer to the list box and a pointer to an array.

// ----------------------------------------------------
// Method: LISTBOX_MANAGE_COL_SIZE
// Description
// Tracks the width of columns in a list box
// Parameters
// $1 (Pointer) - Pointer to list box variable
// $2 (Pointer) - Pointer to array tracking the width of columns in the list box
// ----------------------------------------------------

C_POINTER($1;$listbox_p)
C_POINTER($2;$colWidthArr_p)
C_LONGINT($numColumns_l)

If (Count parameters>=2)

   $listbox_p:=$1
   $colWidthArr_p:=$2

   LISTBOX GET ARRAYS($listbox_p->;arrColNames;arrHeaderNames;arrColVars;\
      arrHeaderVars;arrColVisible;arrStyles)

   $numColumns_l:=LISTBOX Get number of columns($listBox_p->)

   For ($i;1;$numColumns_l)
      $colWidthArr_p->{$i}:=LISTBOX Get column width(arrColVars{$i}->)
   End for

End if


Below is an example of how the array to track the width of the columns can be created and passed into the LISTBOX_MANAGE_COL_SIZE method. The following code should be placed in the list box form method.

Case of
   : (Form event=On Load)
    C_LONGINT(LB_numColumns_l)
    LB_numColumns_l:=LISTBOX Get number of columns(List Box)
    ARRAY LONGINT(arrColWidth;LB_numColumns_l)
    LISTBOX_MANAGE_COL_SIZE (->List Box;->arrColWidth)
   : (Form event=On Column Resize)
    LISTBOX_MANAGE_COL_SIZE (->List Box;->arrColWidth)
   : (Form event=On Column Moved)
    LISTBOX_MANAGE_COL_SIZE (->List Box;->arrColWidth)
End case


Whenever the form is resized, call the method LISTBOX_RESIZE_COLUMN, passing in a pointer to a list box variable, a pointer to an array containing the widths of the list box columns, and a pointer to a variable tracking the size of the form.

Case of
   : (Form event=On Load)
    C_LONGINT(LB_Width_l)
    OBJECT GET COORDINATES(List Box;$left;$top;$right;$bottom)
    LB_Width_l:=$right-$left
   : (Form event=On Resize)
    LISTBOX_RESIZE_COLUMN (->List Box;->arrColWidth;->LB_Width_l)
End case