Call the
methods and catch the exceptions
Where you are
Add a new sheet window
to the existing application
Create user-defined
exception objects
Create a new user
function and user event
> Call the methods
and catch the exceptions
You now write code to populate the drop-down list box controls with
state codes from the customer table in the Demo Database 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 entering the following lines for the losefocus event
script:12345678Trythis.EVENT ue_modified(this.text)Catch (exc_bad_entry le_be)messagebox ("from exc_bad_entry", &le_be.getmessage())End Tryreturn 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. -
Enter the following lines in the Constructor event to populate
the drop-down list box control:12345678910111213141516171819202122232425262728int li_nrows, nstring ls_state//Get the distinct count of all states in the//customer tableSELECT count(distinct state) INTO :li_nrowsFROM customer;//Declare the SQL cursor to select all states//in customer table but avoid//rows with duplicate values for state.DECLARE custstatecursor CURSOR FORSELECT state FROM customerGROUP BY state HAVING count(state)=1UNIONSELECT state FROM customerGROUP BY stateHAVING count(state)>1;OPEN custstatecursor ;//Populate the control with a single entry for//every state in the customer table.FOR n=1 TO li_nrowsFETCH NEXT custstatecursor INTO :ls_state;this.additem( ls_state)NEXTCLOSE custstatecursor ;//Set first item in list as selected itemthis.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. -
Enter the following lines for the Clicked event script:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051Decimal my_resultDouble entry_1, entry_2Int li_int, li_rtnString sel_statesel_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 customerWHERE 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.TRYmy_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=1MessageBox("From exc_low_number", &e_ln.getmessage())CATCH (dividebyzeroerror e_zero)li_rtn = MessageBox("No Customers", &"Terminate Application?", Stopsign!, YesNo!)IF li_rtn=1 THENHALTEND IFEND 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 THENsle_result.text ="Value not calculated for " &+ sel_state + "." + " Try another state."ELSEsle_result.text = String (my_result) + &" % of customers are in " + sel_state + "."END IF