KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Resizing ListBox Height Based on the Number of Elements
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: January 22, 2016

In v15R4, the ListBox has an option to hide extra blank rows if it is not necessary to show.

Listbox: Hide extra blank rows option

If this option is used, the visibility of the ListBox may seem uneven. The utility RESIZE_LISTBOX_HEIGHT set the height of a list box to display maximum number of rows:

// ---------------------------------------------------------------------------
// Name: RESIZE_LISTBOX_HEIGHT
// Description: Method will shrink the number of rows to be visible in a
// ListBox or to a specified number.
//
// Input:
// $1 (TEXT) - List Box name
// $2 (LONGINT) - Number of rows to show (optional)
// ---------------------------------------------------------------------------
C_TEXT($1;$listBox)
C_LONGINT($2;$showRows)
C_LONGINT($left;$top;$right;$bottom;$rows;$rowPixels;$headerPixels)

If (Count parameters>0)
    $listBox:=$1
   
    OBJECT GET COORDINATES(*;$listBox;$left;$top;$right;$bottom)
   
    $rows:=LISTBOX Get number of rows(*;$listBox)
    $rowPixels:=LISTBOX Get rows height(*;$listBox;Listbox pixels)
    $headerPixels:=LISTBOX Get headers height(*;$listBox;Listbox pixels)
   
    If (count parameters=1)
     $bottom:=($rows*$rowPixels)+$headerPixels
    Else
     $showRows:=$2
     $bottom:=($showRows*$rowPixels)+$headerPixels+$top
    End if
   
    OBJECT SET COORDINATES(*;$listBox;$left;$top;$right;$bottom)
End if

Note: The method takes in account that the horizontal scrollbar is not enabled. If it is used, an offset will need to be taken into account of the scrollbar.


Example #1 of shrinking the List Box to show the number of rows initialized:

ARRAY LONGINT(Column1;5)

Column1{1}:=1
Column1{2}:=2
Column1{3}:=3
Column1{4}:=4
Column1{5}:=5




Using the method:

SHRINK_LISTBOX_VIEW ("List Box")


Result after the call:



Example #2 of shrinking the List Box with a specified number:

SHRINK_LISTBOX_VIEW ("List Box";3)


Result after the call: