Visual Basic.NET
--
Questions
--
Followers
Top Experts
I create a windows form with a tabControl that has one tabPage. I am trying to create additional tabPages and its controls at run time. I can see the tabPages, but the controls are not showing on the new tabPages. I am not sure what I might be doing wrong, I already spent one whole day checking my code without any positive results Please Help
Here is my code:
Dim oraACDa As New OracleDataAdapter(strACqry
Dim oraACDs As New DataSet
Dim oraACDt As New DataTable
Dim oraACDrow As DataRow
oraACDa.Fill(oraACDs)
oraACDa.FillSchema(oraACDs
oraACDt = oraACDs.Tables(0)
''MANAGEING THE TABS AVAILABLE
tabsActive = oraACDt.Rows.Count.ToStrin
tabsAvailable = tabcrtlFleet.TabCount
If tabsAvailable > tabsActive Then
'Remove Tabs
tabCount = tabsAvailable - tabsActive
If tabsActive < tabCount Then
tabCounter = tabsAvailable - tabsActive
Else
tabCounter = tabsAvailable - tabCount
End If
If tabCount <= 0 Then
tabCount = tabCount * (-1)
Dim newPage As New TabPage()
For tabsActive = 0 To tabCount
tabcrtlFleet.Controls.Add(
Next tabsActive
Else
For tabsRemaining = 1 To tabCount
If tabsRemaining < 0 Then
Exit For
End If
tabcrtlFleet.Controls.Remo
tabCounter = tabCounter - 1
Next tabsRemaining
End If
Else
'Add Tabs
tabCount = tabsActive - tabsAvailable
tabCounter = tabsActive
Dim newPage As New TabPage()
For tabsRemaining = 1 To tabCount
tabcrtlFleet.Controls.Add(
newPage.Name = "TabPage" & tabcrtlFleet.TabPages.Coun
newPage.Text = newPage.Name
createSubCtrls(tabcrtlFlee
Next tabsRemaining
End If
Private Sub createSubCtrls(ByVal pgTabIndex As Int32)
Try
Dim ctrlsNamesArray As Array = {"lblSN", "lblTAT", "lblTAC", "lblFDate", "lblStation", "lblLP", "lblLastFlight", "lblExtTanks", "lblFloor", "linklblSN", "txtbxTAT", "txtbxTAC", "txtbxFDate", "txtbxStation", "txtbxLP", "txtbxLastFlight", "txtbxExtTanks", "txtbxFloor", "txtbxNote", "dgwChk", "dgwMel", "btnNotes"}
Dim ctrlsLocXArray() As Integer = {6, 115, 193, 314, 432, 523, 628, 738, 846, 30, 96, 190, 309, 431, 514, 627, 738, 872, 322, 9, 9, 431}
Dim ctrlsLocYArray() As Integer = {3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, 27, 27, 27, 247, 54, 276, 248}
Dim ctrlsTextArray As Array = {"Aircraft S/N", "TAT", "TAC", "Flight Date", "Station", "Log Page", "Last Flight #", "External Tanks", "Floor Configuration", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"}
Dim ctrlsSizeXArray() As Integer = {84, 46, 87, 86, 59, 75, 88, 88, 133, 35, 84, 84, 92, 56, 86, 90, 85, 85, 94, 707, 407, 284}
Dim ctrlsSizeYArray() As Integer = {19, 19, 19, 19, 19, 19, 19, 19, 19, 15, 23, 23, 23, 23, 23, 23, 23, 23, 23, 187, 179, 208}
' Create a new instances of all the controls classes.
Dim newLabel As New Label()
Dim newLinklabel As New LinkLabel()
Dim newTextBox As New TextBox()
Dim newDataGrid As New DataGridView()
Dim newButton As New Button()
Dim Arrayindex As Integer = 0
' Add other controls to the collection's internal controltab.
'For each new tab created add the new controls
For Each item In ctrlsNamesArray
tabcrtlFleet.TabPages(pgTa
tabcrtlFleet.TabPages(pgTa
tabcrtlFleet.TabPages(pgTa
'Creating Labels
For labelCreate = 1 To 9
tabcrtlFleet.TabPages(pgTa
With newLabel
.Name = item.ToString & (pgTabIndex + 1)
.Text = item.ToString
.Size = New Drawing.Size(ctrlsSizeXArr
.Location = New Point(ctrlsLocXArray(Array
.Visible = True
End With
'Creating LinkLabel
For LinkLabelCreate = 1 To 1
tabcrtlFleet.TabPages(pgTa
With newLinklabel
.Name = item.ToString & (pgTabIndex + 1)
.Text = item.ToString
.Size = New Drawing.Size(ctrlsSizeXArr
.Location = New Point(ctrlsLocXArray(Array
End With
Next
'Creating DataGridView
For DataGridCreation = 1 To 2
tabcrtlFleet.TabPages(pgTa
With newDataGrid
.Name = item.ToString & (pgTabIndex + 1)
.Text = item.ToString
.Size = New Drawing.Size(ctrlsSizeXArr
.Location = New Point(ctrlsLocXArray(Array
End With
Next
'Creating Textboxes
For TextboxCreate = 1 To 9
tabcrtlFleet.TabPages(pgTa
With newTextBox
.Name = item.ToString & (pgTabIndex + 1)
.Text = item.ToString
.Size = New Drawing.Size(ctrlsSizeXArr
.Location = New Point(ctrlsLocXArray(Array
End With
Next
'Creating Button
For buttonCreate = 1 To 1
tabcrtlFleet.TabPages(pgTa
With newButton
.Name = item.ToString & (pgTabIndex + 1)
.Text = item.ToString
.Size = New Drawing.Size(ctrlsSizeXArr
.Location = New Point(ctrlsLocXArray(Array
End With
Next
Next
Next
Catch ex As Exception
System.Windows.Forms.Messa
End Try
End Sub
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
You use the same code for all the controls, but (Arrayindex) does not change.
Now change the size of that UserControl to what you want and start adding all the controls from the ToolBox to it until it looks like you want it to. Here I've just thrown some controls on it to illustrate:

Now you can create instances of it at run-time and add it to a TabControl. For instance, this snippet creates a new TabPage with the UserControl in it and adds it to TabPage1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TP As New TabPage("Aircraft1")
Dim acData As New AircraftData
acData.Dock = DockStyle.Fill
TP.Controls.Add(acData)
TabControl1.TabPages.Add(TP)
End Sub






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Mike thank you so much for the example you gave me. I'll give it a try!
If all you need is to make it easier to add controls that are aligned and positioned automatically, then adding a FlowLayoutPanel to your TabPage and adding controls to the FlowLayoutPanel might be what you are looking for.
If we had a better idea of what you are trying to achieve instead of what your results are, we might be in a better situation to help you.
I am trying to show the information of each Aircraft on a single tab. Every type of aircraft has a different number of planes; when a specific type of fleet is selected, the tabcontrol should show as many tabs as airplanes are for that specific type.
I am either doing something wrong or not doing something because the tabpages are being created but the controls are not and since I am not getting any error when subroutine creates the controls at run time, it is difficult for me to troubleshoot.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
You are also adding buttons, but do not link them with a Click event, so how would they be useful?
If each tab has the same controls, then the UserControl approach suggested by Mike would be the easiest solution for you.
Visual Basic.NET
--
Questions
--
Followers
Top Experts
Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,