Mastering PowerBuilder

HomePrevious Lesson: Creating Tab Pages Using Custom User Objects
Next Lesson: Tab Control User Objects

Creating Tab Pages Dynamically

Some times, you may not know the number of tab pages that you need to display at run-time. You need to dynamically create tab pages on the fly at run-time and display it to the user. Another reason you may prefer dynamic tab pages is if there are too many tab pages on the tab control, for it takes more time to load the window. Not every user will be using all the tab pages. So, it is a good idea to create only those pages that are needed at run-time, by this, window will be opened faster and also we don't open tab pages unnecessarily.

As you may recall, all the tab pages are listed in the Control[] property of the tab control. We are referring to this property all the time. However, in version 5.0, PowerBuilder DOES NOT populate the dynamic tab page information in the control[] property. Because of this, we need to program tab control in a different way. That is, we need to declare an array of user object variable and refer to those objects in various events & functions.

From version 6.0 onwards, PowerBuilder populates the tab control's Control[] property with the dynamically created pages information.

To create tab pages dynamically, you need to understand OpenTab() and OpenTabWithParm() functions. I will explain these functions as we go. First, create a window 'w_tab_demo4' and place a tab control. As we are going to create tab pages dynamically, we do not need the default tab page. Deleting this tab page is a little tricky. If you select the tab page and hit the delete button, whole tab control is removed from the window. What you could do is, select the default tab page, invoke the popup menu for the tab page (not for the tab control) and select Cut option.

Declare the following instance variables in the window.
uo_trans_for_tab_page_demo iuo[3]
String is_DwObjects[3]

The reason we are declaring three subscripts is, we need to create three tab pages, receipts, issues and returns.

Write the following code to the window open event.
iuo[1] = Create uo_trans_for_tab_page_demo
iuo[2] = Create uo_trans_for_tab_page_demo
iuo[3] = Create uo_trans_for_tab_page_demo
is_DwObjects[1] = "d_receipts_for_tabpage_demo"
is_DwObjects[2] = "d_issues_for_tabpage_demo"
is_DwObjects[3] = "d_returns_for_tabpage_demo"
tab_1.OpenTab( iuo[1], 0 )
iuo[1].Text = "Receipts"
tab_1.OpenTab( iuo[2], 0 )
iuo[2].Text = "Issues"
tab_1.OpenTab( iuo[3], 0 )
iuo[3].Text = "Returns"

In the above code, first we are creating instances of user objects and storing them in the array. In this example, we are using the same user object in all tab pages, however, you can use different user objects. Similarly, we are storing the DataWindow objects that needs to be assigned. Later we are calling OpenTab() function. This function opens the user object as the tab page and inserts before the specified index. We are specifying zero, which means, append this tab page to the existing tab page. That means, the first tab page we open will become the first one. After opening each tab page, we are setting the tab page text by referring to the user object. If PowerBuilder populates tab control's control[] property with dynamic tab page information, we can set the text in the following way.
Tab_1.Control[1].Text = "Receipts"

If you write the above code instead of writing iuo[1].Text = "Receipts", you will be able to compile the code successfully. However, you will end up with run-time error.

See, we haven't painted any thing on the tab control at design time. Now, write the following code to the tab control's SelectionChanged event.
DataWindow ldw1
UserObject luo1
Integer li_ObjectCount, li_Counter
Transaction lTr1
luo1 = iuo[ NewIndex ]
li_ObjectCount = UpperBound( luo1.Control[] )
For li_Counter = 1 to li_ObjectCount
If luo1.Control[ li_Counter ].TypeOf() = &
DataWindow! Then
ldw1 = luo1.Control[li_Counter]
Exit
End If
Next
If Not IsValid( ldw1 ) Then Return -1
//Set transaction object only if it is not already set.
If ldw1.GetTrans( lTr1 ) <> 1 Then
ldw1.DataObject = is_DWObjects[ NewIndex ]
ldw1.SetTransObject( SQLCA )
End If
ldw1.Retrieve()

We are storing the reference to the selected tab page's DataWindow control. Then we are setting the DataWindow object and transaction object if it is not already assigned. Then we are retrieving data into the DataWindow control.

Before we end this topic, one quick point. If you see the tab control's property dialog box, you will find a property Selected Tab. As soon as PowerBuilder creates the tab control, PowerBuilder selects the tab that you specify for this property. That means, SelectionChanged event is fired and this index number is passed to the event.

Remember one thumb rule: Only CUSTOM VISUAL USER OBJECTS can be opened dynamically.
HomePrevious Lesson: Creating Tab Pages Using Custom User Objects
Next Lesson: Tab Control User Objects