4D v14.3

Using object methods

Home

 
4D v14.3
Using object methods

Using object methods  


 

 

You can attach a method to any active object in a form. Methods that are attached to individual objects on a form are called object methods.

The following are some of the more common uses of object methods:

  • Enforcing data entry constraints,
  • Initializing and managing interface objects such as tab controls, pop-up menus, drop-down lists, list boxes, combo boxes, hierarchical lists, and pop-up menus.
  • Specifying the action that takes place when an object is clicked or double-clicked,
  • Managing drag-and-drop operations.

Here are some simple examples that perform operations on data.

The following method calculates a total based on data in two other fields:

 Line_Total:=[Products]Price *[Orders]Quantity

Here is a method to make all characters in a Name field uppercase:

 [Customers]Name:=Uppercase([Customers]Name)

The following method concatenates values from a First Name field and a Last Name field and assigns the results to a variable named vName:

 vName:=[Employees]First Name+" "+[Employees]Last Name

Because each object method is attached to its object, you create object methods from within the Form editor. For information on how to use methods, see Editing methods.

Object methods run when certain events occur. For example, the action associated with a tab control makes sense only when a user clicks a tab. In a scrollable area, you may want the method to execute only when the user double-clicks an item.
You can specify which events will execute the object method for a particular object in the “Events” section of the Property List. The list contains all the form events that are pertinent for the selected object. Some events are only available for specific types of objects. There are also additional events that are only generated at the form level. For a detailed description of all form events, refer to the Form event command in the 4D Language Reference manual. 

You select an event by clicking on the associated check box. To select or deselect all the events at once, press the Ctrl key (Windows) or the Command key (Mac OS) while clicking any event.

If you need to execute different code segments for several different events, use a Case of...Else...End case statement in your method and test for each event you checked in the Events section. To test for an event, you use the Form event function and the Form Events constants. An example shell for an object method might look like this:

 Case of
    :(Form event=On Load)
  //Place code to be executed when the form is opened here
 
    :(Form event=On Data Change)
  //Place code to be executed when the contents of the object are modified here
 
    :(Form event=On Validate)
  //Place code to be executed when the form is validated here
 End case

To create an object method:

  1. In the Form editor, select the object to which you want to assign a method.
  2. Click the Edit... button located next to the Object Method line (“Action” theme) in the Property List.
    OR
    Choose Object Method from the Object menu.
    OR
    Click the object using the right mouse button and choose Object Method from the context menu.
    OR
    Hold down the Alt key (Windows) or Option key (Mac OS) and click the field or object.
    4D displays a new Method editor window, blank by default. The method name is “Object Method:” followed by the form name then the object or field name. 
  3. Write the method as described in Editing methods then close the window or save your work.
    The method is now associated with the field or active object. You can display the objects with an associated method by displaying the “Object Method” shields:

    You can view or modify a method at any time.

To open an object method:

  1. In the Form editor, select the object whose method you want to open.
  2. Click the Edit... button located next to the Object Method line (“Action” theme) in the Property List.
    OR
    Choose Object Method from the Object menu.
    OR
    Right-click the object then choose Object Method from the context menu.
    OR
    Hold down the Alt key (Windows) or the Option key (Mac OS) and click the object to which the method is attached.
    The Method editor appears with your method, ready for you to make any changes.

You can delete an object method at any time. To do so, select the object to which the method is attached and then choose Clear Object Method from the Object menu.

 
PROPERTIES 

Product: 4D
Theme: Building forms

 
ARTICLE USAGE

4D Design Reference ( 4D v14 R2)
4D Design Reference ( 4D v12.4)
4D Design Reference ( 4D v13.4)
4D Design Reference ( 4D v14 R3)
4D Design Reference ( 4D v14.3)
4D Design Reference ( 4D v14 R4)