You write scripts for window and control events. To support these scripts, you can define:
-
Window-level and control-level functions
-
Instance variables for the window
Windows have several events including Open, which is triggered when the window is opened (before it is displayed), and Close, which is triggered when the window is closed. For example, you might connect to a database and initialize some values in the window's Open event, and disconnect from a database in the Close event.
Each type of control also has its own set of events. Buttons, for example, have Clicked events, which trigger when a user clicks the button. SingleLineEdits and MultiLineEdits have Modified events, which trigger when the contents of the edit control change.
Defining your own events
You can also define your own events, called user events, for a window or control, then use the EVENT keyword to trigger your user event.
For example, assume that you offer the user several ways to update the database from a window, such as clicking a button or selecting a menu item. In addition, when the user closes the window, you want to update the database after asking for confirmation. You want the same type of processing to happen after different system events.
You can define a user event for the window, write a script for that event, and then everywhere you want that event triggered, use the EVENT keyword.
To learn how to use user events, see Working with User Events.
PowerBuilder provides built-in functions that act on windows and on different types of controls. You can use these functions in scripts to manipulate your windows and controls. For example, to open a window, you can use the built-in window-level function Open, or you can pass parameters between windows by opening them with the function OpenWithParm and closing them with CloseWithReturn.
You can define your own window-level functions to make it easier to manipulate your windows. For more information, see Working with User-Defined Functions.
In scripts, you can assign values to the properties of objects and controls to change their appearance or behavior. You can also test the values of properties to obtain information about the object.
For example, you can change the text displayed in a StaticText control when the user clicks a CommandButton, or use data entered in a SingleLineEdit to determine what information is retrieved and displayed in a DataWindow control.
To refer to properties of an object or control, use dot notation to identify the object and the property:
object.property
control.property
Unless you identify the object or control when you refer to a property, PowerBuilder assumes you are referring to a property of the object or control the script is written for.
The reserved word Parent
In the script for a window control, you can use the reserved word Parent to refer to the window containing the control. For example, the following line in a script for a CommandButton closes the window containing the button:
close(Parent)
It is easier to reuse a script if you use Parent instead of the name of the window.
All properties, events, and built-in functions for all PowerBuilder objects, including windows, and each type of control are described in Objects and Controls.
Often, data needs to be accessible in several scripts within a window. For example, assume a window displays information about one customer. You might want several CommandButtons to manipulate the data, and the script for each button needs to know the customer's ID. There are several ways to accomplish this:
-
Declare a global variable containing the current customer ID
All scripts in the application have access to this variable.
-
Declare an instance variable within the window
All scripts for the window and controls in the window have access to this variable.
-
Declare a shared variable within the window
All scripts for the window and its controls have access to this variable. In addition, all other windows of the same type have access to the same variable.
When declaring variables, you need to consider what the scope of the variable is. If the variable is meaningful only within a window, declare it as a window-level variable, generally an instance variable. If the variable is meaningful throughout the entire application, make it a global variable.
For a complete description of the types of variables and how to declare them, see the section called “Declaring variables” in PowerScript Reference.
The following assignment statement in the script for the Clicked event for a CommandButton changes the text in the StaticText object st_greeting when the button is clicked:
st_greeting.Text = "Hello User"
The following statement tests the value entered in the SingleLineEdit sle_state and displays the window w_state1 if the text is "AL":
if sle_State.Text= "AL" then Open(w_state1)