Collections are increasingly used in 4D programming. Starting with 4D v19, you can use them to define the content of some interface objects, including through the Form function. This is very useful for generic interface management.
We’ve also added improvements to how lists are used. Let’s see this in detail!
Use collections and lists within forms objects
List usage improvements
List usage has been improved on:
– Hierarchical lists
– Tab controls (first use case)
Collections can now be used as references with the following objects:
– Drop-down lists (Also known as “pop-up lists”)
– Combo boxes
– Tab controls (second use case)
Hierarchical lists & Tab controls
We’ve improved the use of list references for tab controls and hierarchical list form objects. It was already possible to associate the list reference directly to the form object, but this reference can now be retrieved from an object attribute. So now you can write this :
// create a new object
$o:=New object
// add a list to this object
$o.listRef:=New list
// then add items, icons, etc. to this list
// (…)
// then call the dialog using this object (with the included list reference)
DIALOG("myForm";$o)
// clear the list to avoid memory leaks
CLEAR LIST($o.listRef)
Done!
Drop-down lists (pop-ups), Combo boxes & Tab controls (again!)
The main evolution concerns collections. The possible values of the following form objects can be based on a collection of scalar values (usually alpha-numeric), more exactly on an object containing itself a collection. Why? Because it’s generally necessary to know the value selected by the user and/or select a default value (first tab, third item of the drop-down, etc.)
The object variable will therefore contain the following three attributes :
– values: collection of possible values
– index: index of the chosen element (in drop-downs and tabs)
– currentValue: current value (used for combo-boxes, where the user can either select or enter a value)
example
Imagine you want to call a dialog like the one below. But, before calling the dialog, you want to define:
- The content of the Tab Control with “Alpha”; “Bravo”; “Charlie”
- The title (and give the opportunity to enter one)
- The possible choices for “Status”
Each of these form objects will have the same type of associated variable or expression: Object
// prepare a global object that will be sent to the dialog command
$o:=New object
// prepare the tab control object
$o.oTab:=New object
$o.oTab.values:=New collection("Alpha"; "Bravo"; "Charlie")
$o.oTab.index:=0 // select the "Alpha" Tab
// prepare the Title object
$o.oCombo:=New object
$o.oCombo.values:=New collection("Mr"; "Mz"; "Dr")
$o.oCombo.currentValue:=""
// prepare the Status object
// tip : when the index = -1, the currentValue is used as a placeholder !
$o.oPop:=New object
$o.oPop.values:=New collection("Single"; "Married"; "Widdow"; "Divorced")
$o.oPop.currentValue:="Select status…"
$o.oPop.index:=-1
$win:=Open form window("BLOG")
DIALOG("BLOG"; $o)
Run this method and fill the dialog…
Once the dialog is validated (OK is clicked), you’ll be able to know which title and status have been selected or entered.
Here’s the resulting JSON value of the returned object:
Conclusion
Using a single object to immediately define the displayed and selected values is great. You can use the collections in many objects at once and avoid using process or interprocess arrays. Of course, the main goal is also reached: this will make generic programming easier than ever.
As usual, an HDI will walk you through this feature, and the documentation has also been updated.
Enjoy and have fun coding!