In application development, it is often necessary to build forms dynamically. For example, you might want to generate a search form on the fly, tailored to the user’s needs. In 4D, there are two main approaches for building dynamic forms: either constructing the entire form programmatically or adding objects to an existing form layout. For the second option, until now, it was possible to duplicate objects with OBJECT DUPLICATE, and modify the data source with OBJECT SET DATA SOURCE, all using a classic pointer-based approach.
With 4D 20 R10, new and powerful commands, OBJECT SET DATA SOURCE FORMULA and OBJECT Get data source formula, allow developers to bind a formula as the data source of form objects. Moreover, you can dynamically assign formulas to key listbox properties with the LISTBOX SET PROPERTY command such as Current item, Current item position, and Selected items.
This opens the way to a more modern, flexible and readable approach, thanks in particular to expressions such as Form.xx or the classes.
New commands to manipulate the data source property
Two new commands have been added to modify the data source property:
OBJECT SET DATA SOURCE FORMULA ( {* ;} object ; formula)
OBJECT Get data source formula ( {* ;} object) : formula
Example 1: Update the formula
Let’s imagine we have a text input named “myInput” in a form, and we want to bind its data source to the “Form.myText” formula.
$myFormula:=Formula(Form.myText)
OBJECT SET DATA SOURCE FORMULA(*; "myInput"; $myFormula)
Example 2: Retrieve the formula
To retrieve the formula associated with a form object, such as a text input named “myInput”, you can simply write:
$formula:=OBJECT Get data source formula(*; "myInput")
These new commands facilitate the creation of generic, reusable form templates that can be configured dynamically.
Extended Capabilities for ListBoxes
These properties are accessible using the following constants with LISTBOX Get property and LISTBOX SET PROPERTY commands:
- lk current item expression
- lk current item pos expression
- lk selected items expression
Example:
LISTBOX SET PROPERTY(*; "myListBox"; lk selected items expression; "Form.selectedItems")
Conclusion
With OBJECT SET/GET DATA SOURCE FORMULA and the new listbox property constants, 4D provides a new, elegant, and powerful way to build dynamic interfaces.
By embracing formulas and object-oriented programming, you can now create forms that are more adaptable, maintainable, and easier to read.
