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

   Run the application

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.

  1. 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.

  2. Select losefocus ( ) returns long [pbm_cbnkillfocus] in the second drop-down list box.

  3. Call the ue_modified event and catch the exception object that it throws by entering the following lines for the losefocus event script:

          this.EVENT ue_modified(this.text)
    Catch (exc_bad_entry le_be)
          messagebox ("from exc_bad_entry", &             
    End Try
    return 1
  4. Select constructor ( ) returns long [pbm_constructor] from the second drop-down list box in the Script view prototype window for the ddlb_state control.

  5. Enter 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
    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)
    CLOSE custstatecursor ;
    //Set first item in list as selected item
    this.selectitem (1)
  6. 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.

  7. Enter the following lines for the Clicked event script:

    Decimal my_result 
    Double entry_1, entry_2
    Int li_int, li_rtn
    String sel_state
    //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
    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 
    SELECT count(*) INTO :entry_2 FROM customer;
    st_2.text="Total number of customers: " &
          + string(entry_2)
    //Call uf_percentage and catch its exceptions.
          my_result = uf_percentage (entry_1, entry_2)
    CATCH (exc_no_rows e_nr )
          MessageBox("From exc_no_rows",       &
    CATCH (exc_low_number e_ln )
          MessageBox("From exc_low_number", &             
       CATCH (dividebyzeroerror e_zero)
          li_rtn = MessageBox("No Customers", &
             "Terminate Application?", Stopsign!, YesNo!)
          IF li_rtn=1 THEN
          END IF
    //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."
          sle_result.text = String (my_result) + &
             " % of customers are in " + sel_state + "."
    END IF