Relative Addresses


Some examples and descriptions of relative addresses.

The aliases for several relative addresses:


The current object


The parent of an object


The form that contains the controls etc..


The top most form or browse.

Aliases, like incomplete address, let you specify object names relative to the current object path. This has the same benefit of making code more portable between forms. Using the NEXT and PREV aliases it is possible to iterate through a container's objects. For example you can use iteration to set the properties of all the objects on a form. In order to actually do this you need to also use Pointer addressing. See Pointer Addresses for details.

Another way of iterating through all of the objects in a container is to use the <FORM>.CHILD() and <FORM>.CHILDREN() methods. For example, this script sets the font color of all object to red:
dim obj as P
obj = topparent.this    'get a pointer to the top form
count = obj.children()   'find out how many child objects it has
for I = 1 to count
    child_obj = obj.child(I)
    'check if object is a text box, field or button. If so, set the font.
    if (class $ "text field button") then
        child_obj.font.color = "red"
    end if
next i

Example 1

Assume that you have a button (called B1) on a form (called F1). The following code could be attached to the OnPush event for the button to change the button font to bold:

:F1:B1.font.bold = .T.

However, this code would not work if the form name or the button name was changed. The following code, using aliases, does the same thing, but does not break if an object is renamed:

This.font.bold = .f.

Example 2

Assume that you have a form called Customer. It contains a button that displays the next record. The code for the button (not using aliases) could be:


Now, consider the following situation: Assume that the user opens a second instance of the Customer form. Alpha Anywhere will name this second instance of the customer form Customer1 because it will not allow duplicate object names. Now assume that you press the Next Record button on this second instance of the form. The next record will not be displayed, as you would expect. Instead, the first instance of the Customer form will display the next record. This is because the code uses an absolute address that references the Customer object. The following code also advances to the next record, but it uses aliases, and therefore it will work for the second and subsequent instances of the form.


The ParentForm alias in this script refers to the button's parent. In the case of the first instance of the Customer form, this is Customer. In the case of the second instance of the Customer form, ParentForm refers to Customer1. Picture


Example 3

Assume that a form called F1 contains a sub-form called SubF1. SubF1 has a button called B1. A script attached to B1 that referred to ParentForm would be referring to SubF1, while a reference to TopParent would be referring to F1.

See Also