4D v14

Video script

Home

 
4D v14
Video script

Video script  


 

 

In this video, we're going to learn how events work and how to program them.

First of all, let's distinguish between:

  • "form" events that are listed in the form properties
  • and "database" events that are defined in the properties of a table because they are associated with triggers; in other words, programming that is executed when specific events occur.

Form events are only triggered when a form is used (on screen or when printing), therefore when the interface is being used by a user (clicks, drag-and-drop, selection of menu items, keystrokes and so on).

Database events only concern 3 actions that are performed on the data:

  • creation
  • modification
  • and deletion.

These 3 events are intercepted by the database engine. This is important to note because in Client/Server mode, the database engine runs on the server so you will never see a database event performed on your client machine. You won't be able to trace one from a client machine either. We'll come back to database events later on.

First of all, we're going to have some fun and create a button that's a little "special".

  • Go to page 4 of the Navigation form, the preferences page
  • Draw a standard button
  • Modify its properties as follows:
  •      - Name the button bSpecial
  •      - Only the “On Mouse Enter” event remains checked
  • Edit its method (ALT-Click on the button)

and enter the following code:

  //Location of the mouse when the method starts
 GET MOUSE($Mouse_H;$Mouse_V;$MouseButton//Where is the object in the window?
 OBJECT GET COORDINATES(bSpecial;$Obj_L;$Obj_T;$Obj_R;$Obj_B)
 
  //Calculate the center of the object
 $Center_H:=$Obj_L+(($Obj_R-$Obj_L)/2)
 $Center_V:=$Obj_T+(($Obj_B-$Obj_T)/2)
 
 $Shift:=10 //Plan an additional shift of X pixels
 
 If($Mouse_H<$Center_H//If the mouse is left of center
    $T:=$Mouse_H-$Obj_L+$Shift  //Move the object to the right
 Else //The mouse is right of center
    $T:=-($Obj_R-$Mouse_H+$Shift//Move object to left
 End if
 If($Mouse_V<$Center_V//If the mouse is above the center
    $B:=$Mouse_V-$Obj_T+$Shift  //Move downward
 Else //The mouse is below center
    $B:=-($Obj_B-$Mouse_V+$Shift//Move up
 End if
 
  //Move the button according to the information set above 
 OBJECT MOVE(bSpecial;$T;$B)

this all means that:

  • we calculate the position of the mouse 
  • and the position of the object (the button) to determine where it's center is
  • we plan to shift the object a certain number of pixels
  • and then we're going to calculate where the mouse is with respect to the center of the button, horizontally and vertically
  • then we can just move the button position

Let's see what this gives us:

  • Display the form (using the Navigator method)
  • Go to the preferences page
  • And try to click on the button.

Each time you get near the button, it moves in the opposite direction (left, right, up or down).

If the button falls off the screen, close the form and start again; the button will re-appear in its initial location.

So that was one example of using events.

Let's look at another one:  when entering an intervention, we want to calculate its duration based on the information entered in the start time and end time.

We will need to:

  • first add an End time field to the table.
    To save time, copy the Time field and paste it in the same table and then change its name.
  • Next we'll add it to the input form by duplicating the start time field and adapting the field concerned: the intervention end time.
  • We must also add the duration to check that our programming works properly.
  • Then we must indicate that the duration must be recalculated when one of the time fields is modified.
  • So we check the "On Data Change" event and indicate the calculation to perform.
    Warning: ALT-Clicking on 2 objects that do not have methods only creates the method for the object that was clicked.

The duration is recalculated as being the difference between the end time less the intervention time, on the condition that the end time is filled in (in other words, not zero).

We can copy the code and now create the end time method.

When we're going to modify it: the intervention time must be filled in and the calculation of the duration will always be the same.
Of course, we're going to check that times are enterable and then we'll test on an intervention:

  • We take all the interventions,  
  • Then we take the first one

The programming was actually executed when we exited the field; in other words, in the context of the "On Data Change" event.

In the next section, we're going to cover programming arrays.

 
PROPERTIES 

Product: 4D
Theme: Events

 
ARTICLE USAGE

Self-training ( 4D v13)
Self-training ( 4D v14)