Tech Tip: Programmatically Create Multiple Forms with Custom Properties in Project Mode
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: July 27, 2020
This is a method to create multiple forms with customized form properties in project mode.
//--------------------------------------------------------------------------------- // Name: util_create_form // Description: Method will create forms with default form properties settings // unless specified by passing in an object with custom form property settings // // Parameters: // $1 (POINTER) - Array of names for the forms // $2 (OBJECT) - Object containing form properties and its setting (optional) // -------------------------------------------------------------------------------- C_POINTER($formNamesARR;$1) ARRAY TEXT($existingFormsARR;0) C_LONGINT($i;$found_l) C_TEXT($formAttr_t;$db_t) C_OBJECT($formAttr_o;$ob;$2) ARRAY TEXT($obAttrARR;0) ARRAY TEXT(formAttrARR;0) If (Count parameters>=1) $formNamesARR:=$1 $formAttr_t:="{\"$4d\":{\"version\":\"1\",\"kind\":\"form\"},\"windowSizingX\"\ :\"variable\",\"windowSizingY\":\"variable\",\"windowMinWidth\":0,\ \"windowMinHeight\":0,\"windowMaxWidth\":32767,\"windowMaxHeight\"\ :32767,\"rightMargin\":20,\"bottomMargin\":20,\"events\":[\"onLoad\",\ \"onPageChange\",\"onVa"+"lidate\",\"onClick\",\"onDoubleClick\",\ \"onOutsideCall\",\"onBeginDragOver\",\"onDragOver\",\"onDrop\",\ \"onAfterKeystroke\",\"onMenuSelect\",\"onPluginArea\",\"onAfterEdit\",\ \"onTimer\",\"onBoundVariableChange\"],\"windowTitle\":\"window title\",\ \"destination\":\"detailScreen\",\"page"+"s\":[{\"objects\":{}},{\"objects\"\ :{}}]}" If (Count parameters=2) $ob:=$2 $formAttr_o:=JSON Parse($formAttr_t) OB GET PROPERTY NAMES($ob;$obAttrARR) OB GET PROPERTY NAMES($formAttr_o;formAttrARR) For ($i;1;Size of array($obAttrARR)) If (Find in array(formAttrARR;$obAttrARR{$i})#-1) $formAttr_o[$obAttrARR{$i}]:=$ob[$obAttrARR{$i}] End if End for $formAttr_t:=JSON Stringify($formAttr_o;*) End if FORM GET NAMES($existingFormsARR) For ($i;1;Size of array($formNamesARR->)) If (Find in array($existingFormsARR;$formNamesARR->{$i})=-1) $db_t:=Get 4D folder(Database folder)+"Project"+Folder separator+"Sources"+Folder separator+"Forms" CREATE FOLDER($db_t+Folder separator+$formNamesARR->{$i}) TEXT TO DOCUMENT($db_t+Folder separator+$formNamesARR->{$i}+Folder separator+"form.4DForm";$formAttr_t) End if End for ALERT("Complete") End if |
Here is an example if you wanded to create 2 forms named "myform1" and "myform2", both forms with only the "on load" form event turned on. Pass the array of form names as the first parameter and pass the form properties setting as the second parameter.
ARRAY TEXT($formNamesARR;0) APPEND TO ARRAY($formNamesARR;"myform1") APPEND TO ARRAY($formNamesARR;"myform2") C_OBJECT($ob) $ob:=New object("events";New collection) $ob.events.push("onLoad") util_create_form (->$formNamesARR;$ob) |
Below are the supported form properties that can be modified along with their default value:
"windowSizingX": "variable",
"windowSizingY": "variable",
"windowMinWidth": 0,
"windowMinHeight": 0,
"windowMaxWidth": 32767,
"windowMaxHeight": 32767,
"rightMargin": 20,
"bottomMargin": 20,
"events": ["onLoad","onPageChange","onValidate","onClick","onDoubleClick","onOutsideCall","onBeginDragOver",
"onDragOver","onDrop","onAfterKeystroke","onMenuSelect","onPluginArea","onAfterEdit","onTimer",
"onBoundVariableChange"],
"windowTitle": "window title",
"destination": "detailScreen",
"pages" : [
{
"objects":{}
},
{
"objects":{}
}