Writing scripts in windows
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
About events for windows and controls
Windows themselves 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 you offer your 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 as well after asking for confirmation. So
here you want the same type of processing to happen after different
events.
You could define a user event, such as UpdateDB, for the window,
write a script for that event, then everywhere you want that event
triggered, use the EVENT keyword.
To learn how to use user events, see Chapter 6, “Working with User Events “.
About functions for windows and controls
PowerBuilder provides built-in functions that act on windows
and built-in functions that act on types of controls. You can use
these functions in scripts to manipulate your windows and controls.
For example, to open a window, you use the built-in window-level
function Open.
Passing parameters You can pass parameters between windows by opening them with
the function OpenWithParm and closing them with CloseWithReturn.
For more information, see the PowerScript
Reference
.
You can define your own window-level functions to make it
easier to manipulate your windows.
For more information, see Chapter 5, “Working with User-Defined Functions “.
About properties of windows and controls
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 the information that 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:
|
1 |
<i>object.property</i> |
|
1 |
<i>control.property</i> |
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 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:
|
1 |
close(Parent) |
It’s easier to reuse a script if you use Parent instead
of the name of the window.
Objects and Controls
lists
all properties, events, and built-in functions for all PowerBuilder objects
(including windows) and each type of control.
Declaring instance variables
Often you have data that 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; each of them 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 for 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.
The best approach
When declaring variables, you need to consider what the scope
of the variable is. If the variable is only meaningful 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 more information
For a complete description of the types of variables and how
to declare them, see the PowerScript Reference
.
Examples of statements
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:
|
1 |
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”:
|
1 |
if sle_State.Text= "AL" then Open(w_state1) |