4D v16

Arrays, pop-ups, list boxes

Home

 
4D v16
Arrays, pop-ups, list boxes

Arrays, pop-ups, list boxes    


 

 

In addition to the manipulations explained in the video, you should also note that:

Arrays are one of the indispensable features in 4D.

Practical, unlimited, dynamic, one- or two-dimensional, they are a “space” in memory that can be displayed in forms using objects (pop-ups, combo boxes, list boxes, scrollable areas, etc.

We’ve already discussed the concept and use of single-value variables. An array is a multi-valued variable that we can read or write each of its elements.

We define an array by setting the number of lines it contains and its type. Here’s a chart showing the different stages of a variable and an array’s life cycle:

StepSimple variableArray variable
InitializationC_TEXT(vText)ARRAY TEXT(ArrayText;10) `10 rows
ValorizationvText:="Tascher de la Pagerie"ArrayText{1}:="De Beauharnais" `row 1
ArrayText{2}:="Barras" `row 2
ArrayText{3}:="Bonaparte" `row 3
...
Usage$NumChar:=Length(vText)$Amant:=ArrayText{1}
Erase contentCLEAR VARIABLE(vText)ARRAY TEXT(ArrayText;0)
(behavior is different between an interpreted and compiled
application, refer to 4D documentation)
View on a formGive the variable name to a variable type objectGive the name of the variable to a scrollable area/pop-up menu type object
TypesInteger, Long integer, Numeric, Alpha, Text, Boolean, Date, Time, Picture, BLOB, pointersIdentical to variable types except Time and BLOB

As you can see, there are a number of similarities between the two.

The name of the array is sometimes used with braces { }, sometimes by itself. In this case, it’s a variable (long integer) automatically created by 4D.

This variable, associated with the array, serves as an array index (row number). It’s through this variable that we can learn which row the user selected or force selection of a specific row in the pop-up menu.

This is what you’ll often see this concise syntax written in database:

 [INTERVENTIONS]Object:=ObjectsArr{ObjectsArr}

that we can decrypt as follows: "Object := array content {at the chosen row}"
You’ll also find, though more concise and a lot more generic, this syntax that uses the Self command (pointer towards the object whose method is being executed):

 [INTERVENTIONS]Object:=Self->{Self->}

Regardless of the syntax used, the behavior is the same.

In 4D, a tab is an unique object with several titles (values). It's an example of an interface object that can represent an array.

Usually, we put tabs on page 0 of the form (see the section that covers this point).

You'll notice that arrays become useful quite quickly; in fact, they soon become a necessity.

An array only contains items of the same type. You cannot have an array with an Alpha element, a Date element and then a third Time element. In thiscase, you can use an array of pointers that could point to variables of different types.

As mentioned in the lesson on pointers, you can combine pointers and arrays to get “pointer arrays”.

You can also consider a List box to be a series of connected arrays (of the same X dimension).

It's an object that groups together and synchronizes one or more arrays.

In a list box, you can configure:

  • the list box itself
  • each column header
  • and each column

In all, if your list box has X columns, you have 2X+1 objects (X columns, X headers + 1 list box).

List boxes let you:

  • enter data
  • sort and move rows and columns
  • display alternating colors
  • have a hierarchical display
  • add totals to footers
  • A list box can be synchronized with arrays as we did here or with fields of the current selection (or a named selection) of a table
  • and so on.

Keep in mind that the list box synchronizes its columns; it takes the smallest number of rows from the arrays that make it up.

This is important to remember because you could have arrays that are filled with data and still end up with an empty list box if one of its arrays is empty.

Today we're going to learn to create and program arrays as well as objects we can use to represent them in forms.

Like a simple variable, an array must be:

  • declared
  • assigned a value
  • and then used.

To help us understand this, we're going to create a first array in the DETAIL form of the INTERVENTIONS table.

This array will concern the objects.
So, we will need to:

  1. Create the array in memory
  2. Place an object on the form that can represent this array in memory
  3. Check that the array contains the correct information
  4. Then when we select a value in the array, we transfer this value to the field

To make things simple, we're going to put all the programming in the object. This will also let us review the concept of an event.

  • Select the popup/drop-down list object
  • Then draw it to the right of the OBJECT field.
  • Name it PopObjects
  • We check the On Load and On Clicked events
  • Then we edit the method as follows.

 $evt:=Form event
 Case of
    :($evt=On Load//before the form is displayed
       ARRAY TEXT(PopObjects;5) //Definition of array (Type, Name and number of elements)
 
  //Fill in the elements of the array
       PopObjects{1}:="Training"
       PopObjects{2}:="Software"
       PopObjects{3}:="Hardware"
       PopObjects{4}:="Network"
       PopObjects{5}:="System"
 
    :($evt=On Clicked//when the user chooses an element in the array
       If(PopObjects#0) //if the user chose an element
          $ChosenElement:=PopObjects //store the selected element
          $ChosenValue:=PopObjects{$ChosenElement//store the value contained in this element
          [Interventions]Object:=$ChosenValue  //assign the value to the field
       End if
 End case

The object method will be made up of a certain number of elements:

  • Initially, the test of the event.
  • If it is "On Load", we create an array of 5 elements, where the 1st will contain "Training" and so on
  • And when we click on the object, the transfer will take place here in the object.

We're going to trace the operation of this method by carrying out an initial test.

We see that when we double-click on an intervention, we go to On Load.

We'll be able to display the array that contains 5 elements and these 5 elements are gradually assigned values.
The intervention appears.

When we choose a value in the menu, we see here the line number indicated in a variable, which is a variable that has the same name as the array, but which is a longint type variable.

And here we see the 5 elements of the array, the elements 1 to 5 and then the zero element which we will come back to later.

When this method is executed:

  • the event concerned is "On Clicked".
  • we can check that an element has been chosen.
  • if the element was chosen, we can:
    •  pass by the intermediate variable (here "ChosenElement")
    • retrieve the chosen value, if we look at ChosenElement, we see that it is "Hardware"
    • and transfer this chosen value to the object.

We can replace these 3 lines by a single one as follows:

 [Interventions]Object:=PopObjects{PopObjects}

Why?

Because this value here is the longint variable that contained the 3 earlier ones, so it indicates the line number of the array concerned (which has the same name).
For generic issues, you'll often find this type of syntax:

 [Interventions]Object:=Self->{Self->}

which uses a pointer to the object.

Since programming is carried out on the object itself, Self refers to the object and when we write Self->{Self->},

we indicate the array {at the row chosen in the array} and we transfer the contents directly into the object.

We're going to trace this to check that we get the same result.

  • So currently we have "system" in the [intervention] object.
  • If we display PopObjects{PopObjects} we have "system"
  • If we display Self->{Self->} we also have "system"

This breakdown helps us understand in detail.
This is the most generic and concise way of writing it.

Now we're going to create a new array where we can enter a date to within plus or minus 10 days.

  • Duplicate the object
  • Rename it
  • Copy
  • And modify its method.

This is a date array of 21 elements: today + 10 days - 10 days
and we'll write the most concise code this way.

So, how do we fill in these elements? We're going to make a loop where we assign a value to the array as follows: (Current date + $i-11).

Of course, the array will have the correct name and now we can test it:
and we see here that when we choose a value in the array, it is shifted next to the intervention date.

Now that you know how to program arrays, you can represent them in different 4D objects, such as list boxes.

A list box is an object made up of one or more columns with headers and it is precisely the column that interests us, where we'll just write the name of the array that we managed in memory.

If we now return to an intervention, the list box contains the different values of the array and we see the direct synchronization between the choice in the list box and the corresponding one in the array here.

We're going to program the list box in the same way so that when we click on it or when we make a new selection in it, the value of the object is modified automatically.

So in the list box we check the On Clicked and On Selection Change events.

You just need to copy the following line into the object method of the list box:

 [Interventions]Object:=PopObjects{PopObjects}

During use, when we select a value in the list box with the mouse or using the arrow keys, the values are transferred automatically.

Of course, it is also possible to use drag and drop as long as the areas were set as draggable and droppable.

 
 

 
PROPERTIES 

Product: 4D
Theme: Arrays, pop-ups, list boxes

 
HISTORY 

 
ARTICLE USAGE

Self-training ( 4D v16)