Sometimes setting an attribute link between two attributes
does not provide the complete behavior that your component or application
needs. In these situations, you might want to define your own event
handler to listen for an “attributeName Changed” event
and to perform the desired behavior in the event handler method.
For example, consider
a frame with a list box that contains the names of columns in a SAS
table. A text entry control is also on the frame. As a user of the
frame selects one or more columns in the list box, the text entry
control displays a string that contains the list of selected columns,
with blank characters separating the column names. The standard attribute
linking functionality cannot handle this requirement because of the
type mismatch of the attributes that are involved. (The list box stores
selected columns as list items in its
selectedItems
attribute. The text entry control displays the text as a character
string in its
text
attribute.) You
must write code to convert the list of items in
selectedItems
to a concatenated character string that can be displayed in the
text entry control.
The following example
demonstrates how you can create a subclass of the text entry control
to provide such functionality:
-
Using the Class Editor,
create a new class whose parent is
sashelp.classes.textentry_c.class
and whose description is
Smart Text Entry
.
-
Save the class as
sasuser.myclasses.SmartTextEntry.class
.
-
Right-click on the
Event Handlers node and select
New Event
Handler. Add an event handler whose Event Generator is
“Any Object (*)”. Set the Event Name to
selectedItems Changed
and the Method Name to
onSelectedItemsChanged
.
-
When you are prompted
to add the new method, click
Yes. In the
New Methods dialog box, click the ellipsis (...) button to modify
the Signature field. In the Signature dialog box, click the
Add button to add a new argument whose type is a specific
class name, and then enter
sashelp.classes.attributechangedevent
. Click
OK, and then click
OK again to close the New Method dialog box and return to the Class
Editor.
-
Select the
Methods node, and then select the new onSelectedItemsChanged
method. Right-click and select
Source from
the pop-up menu, and then add the following SCL code:
useclass sasuser.myclasses.SmartTextEntry.class;
onSelectedItemsChanged: public method
eventObj:sashelp.classes.attributeChangedEvent.class;
dcl char(500) textstr,
num i,
list localList;
localList = eventObj.value.listValue;
do i = 1 to listlen(localList);
textstr = textstr||' '||getitemc(localList,i);
end;
text=textstr; /* set the text attribute */
endmethod;
enduseclass;
Compile and save the SCL entry.
-
Save the class and close
the Class Editor.
You can then use the
new Smart Text Entry component in conjunction with a list box control.
To create a frame that hosts the Smart Text Entry component:
-
-
Add the text entry subclass
to the Components window by selecting
Add Classes from its pop-up menu. Select or enter
sasuser.myclasses.SmartTextEntry.class
.
The class named Smart
Text Entry appears in the Components window.
-
Drag a list box control
onto the frame. Open the
Properties window
and set the list box control's
selectionMode
attribute to “Multiple Selections”.
-
Drag and drop a Variable
List Model component onto the list box to establish a model/view relationship.
In the
Properties window, set the model's
dataSet
attribute to a valid SAS table such as
sashelp.prdsale
.
The list box should
immediately be populated with the column names in the table.
-
Drag and drop a Smart
Text Entry component onto the frame. Resize the component as necessary.
-
Select
BuildTest to test the frame.
As you make multiple
selections from the list box, the text entry control is automatically
updated to display a string of selected items. Because the list box
control's
selectedItems
attribute was
defined in the class with SendEvent=Yes, the selectedItems Changed
event is sent each time the attribute value is changed. The event
also includes an instance of the Attribute Changed Event component
that contains information about the attribute and its value. The event
handler on the Smart Text Entry component retrieves the new value
of the Attribute Changed Event component that is passed to the method.
The method then loops through the items in the list to create a string
of column names that are displayed in the text entry.