Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TabControl and dynamically adding tabpages

Status
Not open for further replies.

NatHunter

Technical User
Aug 17, 2001
51
0
0
GB
Hi Folks,

I am looking at using a TabControl to display records from a database. The basic gist is that I pull a selection of records out of a database and add a tabpage to the tabcontrol for each one (code shown below). One field is used for the tabpage.text and the other is to be displayed in a text box on the new tab page.

My problem is that the database record text is assigned to the text box, but when I tab around, the text is not seen. If I create a text box at design time for a tabpage, it is seen when I click on a tab.

Any ideas?!

Thanks in advance

Paul


Dim tpDetails As New TabPage
Dim txtTab As New TextBox

For Each rowDets In dsCardFile.Tables("Details").Rows

If Not IsDBNull(rowDets("Notes")) Then
txtTab.Text = rowDets("Notes")
End If
tpDetails.Controls.Add(txtTab)
tpDetails.Text = rowDets("Name")
tpDetails.Name = rowDets("Name")
FrmCardFile.tbDetails.TabPages.Add(tpDetails)

Next
 
I tested this code and it worked fine. Are you sure that the text from the database is being assigned to the textbox?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for taking the time to look at this.

Interesting - yes, the text from the database is being assigned to the txttab textbox - I've used 'Debug.Print(txtTab.Text)' to verify this.

My declarations in the sub are:

Dim rowDets As DataRow
Dim tpDetails As New TabPage
Dim txtTab As New TextBox

I can't even see the text boxes on each tab - there must be something obvious that I'm missing. Do I have to do anything to 'activate' the text boxes when a tab is selected, for example?!

Paul
 
Ok, I was a little misleading in my first response. I did not do your code in a loop, it was just a button click event that added one tabpage and one textbox per click. I modified the code to do this in a loop, and I got the same results you did.

However, I have now found a solution. Do this and it should work (changes in red):

[red]Dim tpDetails As TabPage 'remove 'New' keyword
Dim txtTab As TextBox 'remove 'New' keyword[/red]

For Each rowDets In dsCardFile.Tables("Details").Rows
[red]tpDetails = New TabPage
txtTab = New TextBox[/red]

If Not IsDBNull(rowDets("Notes")) Then
txtTab.Text = rowDets("Notes")
End If
tpDetails.Controls.Add(txtTab)
tpDetails.Text = rowDets("Name")
tpDetails.Name = rowDets("Name")
FrmCardFile.tbDetails.TabPages.Add(tpDetails)

Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Great stuff - works a treat.

Not quite got it straight in my mind as to why this works, but probably due to the fact that I'm trying to add the same control to more than one tabpage.

Thanks again for your time and effort in solving this!

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top