4D v16Events |
||
|
4D v16
Events
Events
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:
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.
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:
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:
A field or a variable on which you click:
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: $evt:=Form event 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:
and of course, the old standbys:
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 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:
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".
and enter the following code: //Location of the mouse when the method starts this all means that:
Let's see what this gives us:
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:
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.
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
HISTORY
ARTICLE USAGE
Self-training ( 4D v16) |