Examples of user object controls affecting a window

To illustrate these techniques, consider a simple custom visual user object, uo_backcolor, that contains two buttons, Green and Blue.

Figure 14-4: User object with two buttons

The sample shows a visual user object with two buttons labeled green and blue

If the user clicks the Green button in an application window containing this user object, the current window background turns green. If the user clicks Blue, the window background color changes to blue.

Because the user object can be associated with any window, the scripts for the buttons cannot reference the window that has the user object. The user object must get the name of the window so that the buttons can reference the window.

“Example 1: using functions” shows how PocketBuilder uses functions to pass a window name to a user object, allowing controls in the user object to affect the window the user object is in.

“Example 2: using user events” shows how PocketBuilder uses unmapped user events to allow controls in a user object to affect the window the user object is in.

Example 1: using functions

  1. In the Script view in the User Object painter, define an instance variable, mywin, of type window.

    window mywin
    

    This variable will hold the name of the window that has the user object.

  2. Define a user object-level function, f_setwin, with:

  3. Type the following script for the function:

    mywin = win_param
    

    When f_setwin is called, the window name passed in win_param will be assigned to mywin, where user object controls can reference the window that has the user object.

  4. Write scripts for the two buttons:

  5. Save the user object as uo_backcolor and close the User Object painter.

  6. Open the window, select Insert>Control>User Object, select uo_backcolor, and click in the window in the Layout view to add the user object.

  7. In the Properties view, change the name for the user object control to uo_func.

  8. In the Open event for the window, call the user object-level function, passing the name of the window:

    uo_func.f_setwin(This)
    

    The pronoun This refers to the window's name, which will be passed to the user object's f_setwin function.

What happens When the window opens, it calls the user object-level function f_setwin, which passes the window name to the user object. The user object stores the name in its instance variable mywin. When the user clicks a button control in the user object, the control references the window through mywin.

Example 2: using user events

  1. In the Script view in the User Object painter, define two unmapped user events for the user object: Green_requested and Blue_requested.

    Leave the Event ID fields blank to define them as unmapped.

  2. Trigger user events of the user object in the scripts for the Clicked event of each CommandButton:

  3. Save the user object and name it uo_event and close the User Object painter.

  4. Open the window and, in the Window painter, select Insert>Object from the menu bar and then place uo_event in the window.

  5. Double-click uo_event to display its Script view.

    The two new user events display in the second drop-down list in the Script view.

  6. Write scripts for the two user events:

    These scripts reference the window containing the user object with the pronoun Parent.

What happens When a user clicks a button, the Clicked event script for that button triggers a user event in its parent, the user object. The user object script for that event modifies its parent, the window.