In addition to the manipulations explained in the video, you should also note that:
Not all objects have the same events: for example, you cannot check the "On Data Change" event for a button since you cannot "enter" its contents (title).
Here are a few examples:
On Mouse Enter When the mouse enters an object's “air space”.
On Data Change At the exit of a field or variable whose content changed. This is primarily used for checks on input, formatting (upper- and lowercase), queries, re-calculations.
On Clicked When an object is clicked (mainly buttons, popups, menus, etc.) Can also be used on enterable objects.
On Getting Focus The object just received focus (from On Clicked, used with the tab key or with programming).
On Load Just before a form is displayed on the screen or used to print. During this event initialization is usually performed.
On Printing Detail During record printing. Allows, for example, setting a value for total, concatenation variables.
This list helps you understand events. You can refer to the 4D documentation for a complete description of events.
There’s one important detail that new users always ask: “Do I need to check form events or object events?” The response is simple: everything depends on where you are going to create your method. The form method, in theory, should only contain what is relevant to processing the entire form:
resizing
outside call
display/hiding objects
etc.
Whereas object methods should contain what is specific to an object itself (button, field, etc.). That's the general rule and you should have several good reasons before you consider breaking it.
Keep this in mind:
Form method: Centralized, global processing, easy to maintain. Risks being executed more often than needed.
Object method: Specific, adapted, executed only when necessary. It easily allows porting an object using copy-paste (especially if using pointers). Requires duplicating the method of calling the method in each object.
It does happen that we move certain processes into the form method.
For example, you must re-calculate a value depending on 10 enterable parameters. For each modified parameter, you must re-perform the calculation. Rather than put the formula (or method call) in each of the 10 parameter fields, it is possible to move this calculation into the form method.
In this case, it’s easier and centralized; however, it is highly likely that the re-calculation will be done more often than necessary - especially when modifying an area that doesn’t figure in the final calculation. (Note: 4D can let you know which object was modified so we can adapt the calculation according to this information).
It is important to know the order in which events are executed. For example, for a button, events are executed in the following order:
On Mouse Enter
On Mouse Move
On Getting Focus
On Clicked (if any)
On Mouse Leave
On Losing Focus
A field or a variable on which you click:
On Mouse Enter
On Mouse Move
On Getting Focus
On Data Change (if any)
On Losing Focus
On Mouse Leave
To save time when writing your code and to make it more reliable, remember to set up macros (“macros.xml” file).
For example, you can create a macro that writes the following code for you whenever you type “#$evt” in your method:
While you are still learning to use 4D, we recommend that you start off by putting your methods in your objects; you’ll have more flexibility for updating. Then, start by using generic method calls and finally, when everything works properly, see what you can move from your object methods to your form method or project methods. Then you can start passing pointers as well.
To go further: Take a good look at how events work because they will allow you to precisely understand exactly when to execute your programming. You'll also find a number of ideas for powerful first steps by using events such as:
On Drag Over
On Drop
On Selection Change
plus the three ’On Mouse’ events (Move, Leave, Enter)
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.