Mastering PowerBuilder

HomePrevious Lesson: Using a Common Control For All Tab Pages
Next Lesson: Creating Tab Pages Using Custom User Objects

Using Independent Controls For Each Tab Page

In the previous example we used same DataWindow control for all tab pages. In this section let's learn how to use independent DataWindow controls on each tab page.

Create another window 'w_tab_demo2' and add a new tab control to the window. Change the text of the first tab from 'none' to 'Receipts. Name this tab page as 'tabpage_receipts'. Place a DataWindow control on the 'tabpage_receipts'. Name the DataWindow control as 'dw_receitps'. Now, insert a new tab page and name it as 'tabpage_issues'. Change its text to 'Issues'. Place a DataWindow control on the 'tabpage_issues' and name the DataWindow control as 'dw_issues'. Similarly insert one more tab page for 'Returns'.

We are using three DataWindow controls but, we haven't created/assigned DataWindow objects to these DataWindow controls. Paint a DataWindow to retrieve all the receipt transactions and assign that DataWindow object to the 'dw_receipts' DataWindow. That means, the data source for the DataWindow would be:
SELECT * FROM trans WHERE tran_type = 'R'

Similarly paint DataWindow objects for other DataWindow controls and assign them to the appropriate DataWindow controls. Then write the following code to the 'SelectionChanged' event of the 'tab_1' tab control.
DataWindow ldw1
Transaction ltr1
ldw1 = Tab_1.Control[ NewIndex ].Control[1]
If ( ldw1.GetTrans( ltr1 ) ) <> 1 &
	Then ldw1.SetTransObject( SQLCA )
return ldw1.Retrieve()

You can observe that, in the third line we are using Control[] twice. The first control[] has the list of all the tab pages in the tab control. The second Control[] has the list of all controls that you placed on that tab page. As you know, we placed only one control i.e, DataWindow control on the tab page, That's why, I hard coded Control[1]. In real-world projects, you need to check whether that control is a DataWindow control or not and retrieve it only if it is a DataWindow control. The following revised code does that.
UserObject luo1
DataWindow ldw1
Transaction ltr1
Integer li_Counter, li_TotControls
For li_Counter = 1 to li_TotControls
luo1 = Tab_1.Control[ NewIndex ].Control[ li_Counter]
If luo1.TypeOf() = DataWindow! Then ldw1 = luo1
If ( ldw1.GetTrans( ltr1 ) ) <> 1 Then &
	 ldw1.SetTransObject( SQLCA )
return ldw1.Retrieve()
End If
Next
Return 0

You can observe one more technique from the code fragments above. That is, we are checking whether the DataWindow has a transaction object set to it or not. If not, then only we are calling the SetTransObject() function.

Now, to really understand the difference between 'Using a single control on all tab pages' and this method, you need to print the codes and compare them. In the former, we are referring to the DataWindow control directly like any other control on the window. However, we need to go an extra mile to find the correct DataWindow control in the tab page.
HomePrevious Lesson: Using a Common Control For All Tab Pages
Next Lesson: Creating Tab Pages Using Custom User Objects