Adding tabpages to child tab control

Crazyj
07-03-2004, 10:32 AM
Hello,
I have a form with two combo boxes, a button, and a Tab Control (with no tab pages) created a design time. The first combobox (cboProject) populates the tab page on the parent tab control with it's selected text. It also provides the parameter for the select query to fill a dataset and populate the second combo box(cboAddress). cboAddress is meant to populate tab pages on the second tabcontrol created at run time. The button triggers the event that makes all of the above happen (except the population of cboAddress).

Now the problem I have is adding addition tabs to the child tabcontrol. Since it's created at run time I have to create a "new" instance of it every time I try to access it. I need to be able to access the current instance of the control to add pages to it.

hopefully my code is easier to understand than explaination.


Private Sub cboProject_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProject.SelectedIndexChanged

Try
Dim i As Integer
Dim dslots1 As New DataSet
Dim daAddr As New SqlDataAdapter
Dim item As Object = cboProject.SelectedValue
Dim tp1 As New TabPage
Dim tcAddress As AddTabLayer
Dim bnA As Boolean = False
Dim selAddress As String = "SELECT Address FROM tblDrawInspections " & _
"WHERE ProjectName = '" & CType(item, String) & "'"
daAddr.SelectCommand = New SqlCommand(selAddress, cnn)
daAddr.SelectCommand.CommandType = CommandType.Text
daAddr.Fill(dslots1, "tblAddress")
cboAddress.Items.Clear()
cboAddress.Update()

For Each l As DataRow In dslots1.Tables("tblAddress").Rows
cboAddress.Items.Add(l.Item("Address"))
Next


Catch ex As ArgumentOutOfRangeException
Exit Try
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
End Sub

Private Sub btnGetProperty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetProperty.Click
Dim i As Integer
Dim dslots1 As New DataSet
Dim daAddr As New SqlDataAdapter
Dim item As Object = cboProject.SelectedValue
Dim lot As Object = cboAddress.SelectedItem
Dim tp1 As New TabPage
Dim tp2 As New TabPage
Dim tcAddress As New TabControl
Dim bnA As Boolean = False

tp1.Text = CType(item, String)
tp2.Text = CType(lot, String)
'Creates a new tab with child tabcontrol if none exists
If tbcProjectLayer.TabPages.Count = 0 Then
tbcProjectLayer.TabPages.Add(tp1)
tcAddress.Dock = DockStyle.Fill
tcAddress.Alignment = TabAlignment.Bottom
tcAddress.TabPages.Add(tp2)
tcAddress.Visible = True
tbcProjectLayer.TabPages.Item(0).Controls.Add(tcAddress)
Exit Sub
End If
'Loops through parent tab controls tabs
For i = 0 To tbcProjectLayer.TabPages.Count - 1
'If statement to prevent creation of a duplicate tab
If tbcProjectLayer.TabPages.Item(i).Text = tp1.Text Then
'Supposed to loop through tabs on child tab control of parent item(i)
' and exit procedure if match is found or create new tab if not found.
For s As Integer = 0 To tcAddress.TabPages.Count - 1
If tcAddress.TabPages.Item(s).Text = tp2.Text Then
Exit Sub
Else
tcAddress.TabPages.Add(tp2)
End If
Next


bnA = True
Exit For
End If

Next i
'Creates new tab page on parent tab control with new child tab control with one tab
If bnA = False Then

tbcProjectLayer.TabPages.Add(tp1)
tcAddress.Dock = DockStyle.Fill
tcAddress.Alignment = TabAlignment.Bottom
tcAddress.TabPages.Add(tp2)
tcAddress.Visible = True
tbcProjectLayer.TabPages.Item(tbcProjectLayer.TabPages.Count - 1).Controls.Add(tcAddress)
End If
End Sub

This is the section that doesn't work, as I'm trying to access the child tabcontrol with a new instance of the control

If tbcProjectLayer.TabPages.Item(i).Text = tp1.Text Then
'Supposed to loop through tabs on child tab control of parent item(i)
' and exit procedure if match is found or create new tab if not found.
For s As Integer = 0 To tcAddress.TabPages.Count - 1
If tcAddress.TabPages.Item(s).Text = tp2.Text Then
Exit Sub
Else
tcAddress.TabPages.Add(tp2)
End If
Next

Thanks in advance.

Crazyj
07-03-2004, 10:17 PM
I solved it. I cheated, I expanded the generated designer code and declared and inialized my child tabcontrol there. I don't know if thats proper, but it works

Crazyj
07-04-2004, 08:44 AM
Now another problem has popped up. Everything works fine if I add parent tabs with child tabs in order. but when I try to add a child tab to a parent tab that has been created earlier, the procedure places the tab on the last parent created. I believe the problem occurs because the code for the child tabcontrol believes the last parent tab is active. I need a way to set the focus back on a previously created tab and access the tab control on it.

here is my latest modification on the code posted above.


For i = 0 To tbcProjectLayer.TabPages.Count - 1
'If statement to prevent creation of a duplicate tab
If tbcProjectLayer.TabPages.Item(i).Text = tp1.Text Then
tbcProjectLayer.TabPages.Item(i).Refresh()
'Supposed to loop through tabs on child tab control of parent item(i)
' and exit procedure if match is found or create new tab if not found.
For s As Integer = 0 To tbcAddressLayer.TabPages.Count - 1
If tbcAddressLayer.TabPages.Item(s).Text <> tp2.Text Then
If s < tbcAddressLayer.TabPages.Count - 1 Then

ElseIf s = tbcAddressLayer.TabPages.Count - 1 Then
tbcAddressLayer.TabPages.Add(tp2)
Exit For
End If

Else
Exit For
End If
Next
bnA = True
Exit For
End If

Next i


Any help would be greatly appreciated.

Crazyj
07-07-2004, 02:11 PM
Any takers?

Crazyj
07-09-2004, 08:52 AM
I been thinking about my problem, and the only solution thats coming to mind is creating an arraylist of tab controls so I can call my desired by it's item number.

But this leads me to another problem. Since the child tab controls are created at run time, the array item number(s) will be a variable and I will need a way to "tell" the program which control array item I want to access.

The only way I can think of right now is to create my tab control array in a class, declare it a Shared object in declaration section of the form class, declare a shared Integer variable to used as the item number for the array, and then find a way to link that variable to the Parent tabs so I can selectively access the child controls.

I really could use some advise on this as this is a "Show Stopper" for me.

Iceplug
07-09-2004, 11:03 AM
but when I try to add a child tab to a parent tab that has been created earlier, the procedure places the tab on the last parent created.
You say 'tab' in both places, but are they TabControls or TabPages? I'm guessing that the parent tab is a tab control and the child is a tab page... but how many tab controls do you have to begin with? Or do you have a tab control on each tab page or something (I hope not)? Seems like your tab controls shouldn't be mixed up unless you accidentally set one to reference another one. :)

Crazyj
07-09-2004, 12:19 PM
They are tab controls nested on tab pages from the parent tab control.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum