Advanced PowerBuilder

HomePrevious Lesson: Code For The DataWindow Control
Next Lesson: External User Objects

Code For the CommandButton

Write the following code for the cb_transfer_to_right CommandButton`s clicked event:
// Object: cb_transfer in uo_shuffle_rows_between_2_dws
// Event: Clicked
/* Copies all the selected rows to the right side
DataWindow and deletes each row when copied. Later sorts
both the DataWindows on the first column */
Long ll_SelectedRow = 0
string ls_ColName
ll_SelectedRow = dw_left.GetSelectedRow( ll_SelectedRow )
If ll_SelectedRow = 0 Then Return
Do While ll_SelectedRow <> 0
   f_CopyRecord( dw_right, dw_left, ll_SelectedRow )
   dw_left.DeleteRow( ll_SelectedRow )
   ll_SelectedRow = ll_SelectedRow - 1
   ll_SelectedRow = dw_left.GetSelectedRow(ll_SelectedRow )
Loop
ls_ColName = dw_left.Describe("#1.Name")
dw_left.setsort( ls_ColName + " A" )
dw_left.Sort()
ls_ColName = dw_right.Describe("#1.Name")
dw_right.setsort( ls_ColName + " A" )
dw_right.Sort()
If dw_left.rowcount() = 0 Then
   cb_select_all.enabled = False
   cb_transfer_to_right.enabled = False
Else
   cb_select_all.enabled = True
   cb_transfer_to_right.enabled = True
End If
If dw_right.rowcount() = 0 Then
   cb_deselect_all.enabled = False
   cb_transfer_to_left.enabled = False
Else
   cb_deselect_all.enabled = True
   cb_transfer_to_left.enabled = True
End If

In our design, we have allowed the user to select more than one row using the CTRL and SHIFT keys, so we have to take care to transfer all the selected rows. GetSelectedRow() gives the next highlighted row after the argument row number and once we know the selected row, we call the f_copy_record function to copy the specified row from the source DataWindow to the destination DataWindow.

Once the row has been copied over, we delete it from the source DataWindow and sort the destination DataWindow on the first column of the DataWindow. The arguments passed to the Describe() function returns the name of the first column in the DataWindow and we use the SetSort() function to specify the sort criteria, before performing the actual sort, using the Sort()function.

Note that, if you haven't specified a sort criteria when using SetSort() and moreover if you haven't set it before, PowerBuilder will prompt for a sort criteria.

Copy the script to the cb_transfer_to_left CommandButtons's clicked event. After copying, swap the words dw_left and dw_right in the code.
// Object: cb_select_all
// Event: Clicked
dw_left.SelectRow( 0, True )
// Object: cb_select_all
// Event: Clicked
dw_right.SelectRow( 0, True )

To test the custom user object, paint a window and place it in the new window. In the new window's open event, write the following code:
// Object: w_shuffle_test 
// Event: Open
uo_1.uf_initialize("Available Products", &
     "Selected Products ", "d_test", SQLCA )

We assume that you painted a test DataWindow with tabular presentation style and named it d_test. You can take the following data source for the d_test DataWindow.
SELECT product_description FROM product_master

Save the window as w_shuffle_test. Open this window from the Application object's Open event. Append the following line of code to the Open event code:
Open( w_shuffle_test )

Once you complete the testing, make sure to remove the above code and restore the old code.

Now, you can use this custom user object in any window you want, with just a single line of code. This has been a fairly complex example, but you can see that we've made most out of PowerBuilder's object oriented features, and reduced the amount of code we have to write. We've also tried to make the example as generic as possible, so that, you could easily manipulate them for your own applications. You only have to change the database to the one to which you log on and change the DataWindow assignment statements to match the new database.
HomePrevious Lesson: Code For The DataWindow Control
Next Lesson: External User Objects