Mastering PowerBuilder

HomePrevious Lesson: Using Independent Controls For Each Tab Page
Next Lesson: Creating Tab Pages Dynamically

Creating Tab Pages Using Custom User Objects

In the previous method we placed standard window controls on each tab page. In this method, you will learn how to use custom visual user objects on the tab pages. Coding in this method is more complex compared to previous methods. The first one is easy and straightforward. The second method needs more coding. To use this method, you need to have better understanding of PowerBuilder objects, otherwise, the program will end up have heavy run-time errors, such as NULL object references and assigning objects to the wrong variable types and so on.

In this method, what I would like you to do is, first create a custom visual user object and then the window. Invoke the user object painter and select 'New' and select 'Custom' from the "Visual" category. Place a DataWindow control on the work area and save it as uo_trans_for_tab_page_demo. I know we haven't assigned any DataWindow object for that. Don't worry, we will be doing that at run-time.

Create a window w_tab_demo3 and place a tab control 'tab-1' on the window. Turn off Visual & Enabled properties for the first tab page 'tabpage_1'. Now, click on the tab text display area and invoke the popup menu. Select Insert UserObject and select uo_trans_for_tab_page_demo. Name this tab page as tabpage_issues. Similarly create one more for 'returns'. I would like to do something different for the 'receipts'. This will give you clear understanding of tab control programming. This time instead of selecting 'Insert User Object', select Insert TabPage. Name it as 'tabpage_receipts'. Now, Select 'Controls/User Object' from the menu and uo_trans_for_tab_page_demo and place it on the 'tabpage_receipts' tab page.

In summary, we are not using the default tab page. For 'Issues' and 'Returns' we are creating tab pages using user objects. For 'Receipts' we are adding a regular tab page and placing the user object on the tab page. Now, let's see how to program tab control to retrieve data into these DataWindows. Write the following code to the SelectionChanged event of the tab control 'tab_1'.
DataWindow ldw1
UserObject luo1
Integer li_ObjectCount, li_Counter
Integer li_Counter1, li_ObjectCount1
li_ObjectCount = UpperBound( &
	This.Control[ NewIndex ].Control[] )
For li_Counter = 1 to li_ObjectCount
If This.Control[ NewIndex ].&
Control[li_Counter].TypeOf() = UserObject! Then
luo1 = This.Control[ NewIndex ].Control[li_Counter]
li_ObjectCount1 = UpperBound( luo1.Control[])
For li_Counter1 = 1 to li_ObjectCount1
If luo1.Control[ li_Counter1 ].&
TypeOf() = DataWindow! Then
ldw1 = luo1.Control[li_Counter]
Exit
End If
Next
If NOT IsValid( ldw1 ) Then Return -1
ElseIf This.Control[ NewIndex ].&
Control[li_Counter].TypeOf() = DataWindow! Then
ldw1 = This.Control[ NewIndex ].Control[li_Counter]
Exit
End If
Next
If Not IsValid( ldw1 ) Then Return -1
If This.Control[ NewIndex ].ClassName() = &
	"tabpage_receipts" Then
ldw1.DataObject = "d_receipts_for_tabpage_demo"
ElseIf This.Control[ NewIndex ].ClassName() = &
	"tabpage_issues" Then
ldw1.DataObject = "d_issues_for_tabpage_demo"
ElseIf This.Control[ NewIndex ].ClassName() = &
	"tabpage_returns" Then
ldw1.DataObject = "d_returns_for_tabpage_demo"
End If
ldw1.SetTransObject( SQLCA )
ldw1.Retrieve()

After declaring the variables, we are finding the number of objects that are residing on the selected tab page. Then in a loop we are checking object type for each object on the tab page. Remember that we added the tab page for 'Issues' and 'Returns' by inserting the user object. In this case, This.Control[ NewIndex ] refers to the tab page say, tabpage_issues. Since the tab page itself is a user object, it is referring to 'uo_trans_for_tab_page_demo'. This.Control[ NewIndex ].Control[li_Counter] refers to the DataWindow control inside 'uo_trans_for_tab_page_demo'.

However, the above code is not correct for the 'receipts' tab page. In this case, This.Control[ NewIndex ] refers to the tab page 'tabpage_receipts' itself. This.Control[ NewIndex ].Control[li_Counter] refers to the user object 'uo_1' (of type 'uo_trans_for_tab_page_demo') that we placed on the tab page. To refer to the DataWindow control inside the 'uo_trans_for_tab_page_demo' you need to refer to another set of Control[] property. This is being done in the inner FOR loop.

For practice read this topic couple of times more. See the code in action. Then do the same without looking at the code. If you can do it (without cut/paste method), you are ready to program tab controls in real world apps.
HomePrevious Lesson: Using Independent Controls For Each Tab Page
Next Lesson: Creating Tab Pages Dynamically