You now write code to populate the drop-down list box controls with state codes from the customer table in the EAS Demo DB database. Since you made the control editable, an application user can also type in a value for the state code. Before you process a user-entered value, you check to make sure the value conforms to the conditions you set in the ue_modified event, namely that it is two characters in length.
You also add code to the Clicked event of the command button control to process the current state code in the drop-down list box control. In the Clicked event you call the uf_percentage function to calculate the percentage of customers from the selected state and catch all exceptions that can be thrown by the function.
Make sure the w_cust_pct is open in the Window painter and that ddlb_state displays in the first drop-down list box of the Script view.
Select losefocus ( ) returns
long [pbm_cbnkillfocus]
in
the second drop-down list box.
Call the ue_modified event and catch the exception object that it throws by typing the following lines for the losefocus event script:
Try this.EVENT ue_modified(this.text) Catch (exc_bad_entry le_be) messagebox ("from exc_bad_entry", & le_be.getmessage()) End Try return 1
Select constructor ( ) returns
long [pbm_constructor]
from
the second drop-down list box in the Script view prototype window
for the ddlb_state control.
Type the following lines in the Constructor event to populate the drop-down list box control:
int li_nrows, n string ls_state
//Get the distinct count of all states in the
//customer table
SELECT count(distinct state) INTO :li_nrows FROM customer;
//Declare the SQL cursor to select all states
//in customer table but avoid
//rows with duplicate values for state.
DECLARE custstatecursor CURSOR FOR SELECT state FROM customer GROUP BY state HAVING count(state)=1 UNION SELECT state FROM customer GROUP BY state HAVING count(state)>1;OPEN custstatecursor ;
//Populate the control with a single entry for
//every state in the customer table. FOR n=1 TO li_nrows FETCH NEXT custstatecursor INTO :ls_state; this.additem( ls_state) NEXT CLOSE custstatecursor ;
//Set first item in list as selected item
this.selectitem (1)
Select cb_percent from the first drop-down
list in the Script view.
Make sure clicked ( ) returns long [pbm_bnclicked]
displays
in the second drop-down list box.
Type the following lines for the Clicked event script:
Decimal my_result Double entry_1, entry_2 Int li_int, li_rtn String sel_state
sel_state=ddlb_state.text
//Get the number of rows with customers from the
//selected states and place in the entry_1 variable.
//Change the first static control to display this
//number.
SELECT count(*) INTO :entry_1 FROM customer WHERE customer.state=:sel_state; st_1.text="Customers in state: " + string(entry_1)
//Get the total number of customers and place in //the entry_2 variable. //Change the second static control to display this //number.SELECT count(*) INTO :entry_2 FROM customer; st_2.text="Total number of customers: " & + string(entry_2)
//Call uf_percentage and catch its exceptions. TRY my_result = uf_percentage (entry_1, entry_2)CATCH (exc_no_rows e_nr ) MessageBox("From exc_no_rows", & e_nr.getmessage())CATCH (exc_low_number e_ln ) li_int=1 MessageBox("From exc_low_number", & e_ln.getmessage()) CATCH (dividebyzeroerror e_zero) li_rtn = MessageBox("No Customers", & "Terminate Application?", Stopsign!, YesNo!)
IF li_rtn=1 THEN
HALT
END IF END TRY
//Display the message in the text box. Vary the //message depending on whether there is only one
//customer for the selected state or if more than
//one customer resides in selected state. IF li_int=1 THEN sle_result.text ="Value not calculated for " & + sel_state + "." + " Try another state." ELSE sle_result.text = String (my_result) + & " % of customers are in " + sel_state + "." END IF