4D v14.3Arrays and Form Objects |
||
|
4D v14.3
Arrays and Form Objects
Arrays and Form Objects
Arrays are language objects—you can create and use arrays that will never appear on the screen. However, arrays are also user interface objects. The following types of Form Objects are supported by arrays:
While you can predefine these objects in the Design Environment Form Editor using the Default Values button of the Property List window (except for the List box) , you can also define them programmatically using the arrays commands. In both cases, the form object is supported by an array created by you or 4D. When using these objects, you can detect which item within the object has been selected (or clicked) by testing the array for its selected element. Conversely, you can select a particular item within the object by setting the selected element for the array. When an array is used to support a form object, it has then a dual nature; it is both a language object and a user interface object. For example, when designing a form, you create a scrollable area: The name of the associated variable, in this case atNames, is the name of the array you use for creating and handling the scrollable area. Notes:
The following example shows how to fill an array and display it in a drop-down list. An array arSalaries is created using the ARRAY REAL command. It contains all the standard salaries paid to people in a company. When the user chooses an element from the drop-down list, the [Employees]Salary field is assigned the value chosen. Create a drop-down list and name it arSalaries. The name of the drop-down list should be the same as the name of the array. Initialize the array arSalaries using the On Load event for the object. To do so, remember to enable that event in the Property List window, as shown: Click the Object Method button and create the method, as follows: The lines: ARRAY REAL(arSalaries;10) create the numeric array 2500, 3000... 7000, corresponding to the annual salaries $30,000 up to $84,000, before tax. The lines: arSalaries:=Find in array(arSalaries;[Employees]Salary) handle both the creation of a new record or the modification of existing record.
Note: For more information about the array selected element, read the next section. To report the value selected from the drop-down list arSalaries, you just need to handle the On Clicked event to the object. The element number of the selected element is the value of the array arSalaries itself. Therefore, the expression arSalaries{arSalaries} returns the value chosen in the drop-down list. Complete the method for the object arSalaries as follows: Case of The drop-down list looks like this: The following section describes the common and basic operations you will perform on arrays while using them as form objects. You can obtain the current size of the array by using the Size of array command. Using the previous example, the following line of code would display 5: ALERT("The size of the array atNames is: "+String(Size of array(atNames))) You can reorder the elements of the array using the SORT ARRAY command or of several arrays using the MULTI SORT ARRAY command. Using the previous example, and provided the array is shown as a scrollable area: a. Initially, the area would look like the list on the left. b. After the execution of the following line of code: SORT ARRAY(atNames;>) the area would look like the list in the middle. c. After the execution of the following line of code: SORT ARRAY(atNames;<) the area would look like the list on the right. You can add, insert, or delete elements using the commands APPEND TO ARRAY, INSERT IN ARRAY and DELETE FROM ARRAY. Using the previous example, and provided the array is shown as a scrollable area, you can handle clicks in this area as follows: ` atNames scrollable area object method Note: The events must be activated in the properties of the object. While the syntax atNames{$vlElem} allows you to work with a particular element of the array, the syntax atNames returns the element number of the selected element within the array. Thus, the syntax atNames{atNames} means “the value of the selected element in the array atNames.” If no element is selected, atNames is equal to 0 (zero), so the test If (atNames#0) detects whether or not an element is actually selected. In a similar fashion, you can programmatically change the selected element by assigning a value to the array. ` Selects the first element (if the array is not empty) The Find in array command searches for a particular value within an array. Using the previous example, the following code will select the element whose value is “Richard,” if that is what is entered in the request dialog box: $vsName:=Request("Enter the first name:") Pop-up menus, drop-down lists, scrollable areas, and tab controls can be usually handled in the same manner. Obviously, no additional code is required to redraw objects on the screen each time you change the value of an element, or add or delete elements. Note: To create and use tab controls with icons and enabled and disabled tabs, you must use a hierarchical list as the supporting object for the tab control. For more information, see the example for the Count tasks command. While you can handle pop-up menus, drop-down lists, scrollable areas, and tab controls with the algorithms described in the previous section, you must handle combo boxes differently. A combo box is actually a text enterable area to which is attached a list of values (the elements from the array). The user can pick a value from this list, and then edit the text. So, in a combo box, the notion of selected element does not apply. With combo boxes, there is never a selected element. Each time the user selects one of the values attached to the area, that value is put into the element zero of the array. Then, if the user edits the text, the value modified by the user is also put into that element zero. ` asColors Combo Box object method |
PROPERTIES
Product: 4D SEE ALSO ARTICLE USAGE
4D Language Reference ( 4D v11 SQL Release 6) |