Now you add a function that you invoke from the Percentage command button’s Clicked event and an event that is triggered when the focus is changed from the drop-down list box on the w_cust_pct window. The function calculates the percentage of customers living in a particular state. The event processes the current value of the drop-down list box control to make sure it is two characters in length (for the state code).
Open w_cust_pct in the Window painter if it is not already open.
Select Insert>Function from the Window painter menu.
The Script view displays the Prototype window. The first drop-down list box in the Script view displays (Functions) and the second drop-down list box displays (New Function).
Select decimal for the Return Type and type uf_percentage
for Function
Name.
Select integer for the Argument Type and type ai_custbystate
for the
Argument Name.
You will add a second argument in the next step.
Right-click anywhere in the Prototype window and select Add Parameter from the pop-up menu.
Select integer for the second Argument Type and
type ai_totalcust
for
the second Argument Name.
Type exc_no_rows,exc_low_number
in
the Throws box.
Type the following script for the new function:
Decimal my_result exc_no_rows le_nr exc_low_number le_ex
/* Process two integers passed as parameters. Instantiate and throw exceptions if the first integer value is 0 or 1. Otherwise calculate a percentage and return a numeric value truncated to a single decimal place. If the second integer value is 0, catch and rethrow the runtime dividebyzero error during the calculation. */
CHOOSE CASE ai_custbystate CASE 0 le_nr = create exc_no_rows throw le_nr CASE 1 le_ex = create exc_low_number throw le_ex CASE ELSE TRY my_result=(ai_custbystate/ai_totalcust)*100 CATCH (dividebyzeroerror le_zero) throw le_zero End TRY END CHOOSE
return truncate(my_result,1)
Later in this tutorial, you will call the uf_percentage function from the Clicked event on the command button, passing in two integers and processing the return value.
You now add a user event for the drop-down list box that throws the exc_bad_entry exception object when a user-entered state code is not exactly two characters in length.
Select ddlb_state in the first drop-down list box of the Script view and select (New Event) in the second drop-down list box.
Select integer for Return Type and type ue_modified
for
Event Name.
Select string for Argument Type and type as_statecode
for Argument
Name.
Type exc_bad_entry
in
the Throws box or drag it from the System Tree to the Throws box.
Note that the Event ID is (None). You do not select an Event ID for the ue_modified event. If you selected an Event ID, you could not enter user-defined exception objects in the event Throws clause.
Type the following script for the new ue_modified event:
exc_bad_entry le_ex
//Make sure the current text in the drop-down list
//box is two characters in length. Otherwise,
//instantiate the exc_bad_entry exception object and
//throw the exception.
IF len(this.text)<>2 Then le_ex = create exc_bad_entry throw le_ex END IF Return 1
Next you call the ue_modified event and the uf_percentage function, and catch the exceptions thrown by these methods.