• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

controls created dynamically in tabpage are not showing

Hello 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, connection)
                    Dim oraACDs As New DataSet
                    Dim oraACDt As New DataTable
                    Dim oraACDrow As DataRow

                    oraACDa.Fill(oraACDs)
                    oraACDa.FillSchema(oraACDs, SchemaType.Source)
                    oraACDt = oraACDs.Tables(0)

                    ''MANAGEING THE TABS AVAILABLE
                    tabsActive = oraACDt.Rows.Count.ToString
                    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(newPage)
                            Next tabsActive
                        Else
                            For tabsRemaining = 1 To tabCount
                                If tabsRemaining < 0 Then
                                    Exit For
                                End If
                                tabcrtlFleet.Controls.Remove(tabcrtlFleet.TabPages(tabCounter))
                                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)
                            newPage.Name = "TabPage" & tabcrtlFleet.TabPages.Count
                            newPage.Text = newPage.Name
                            createSubCtrls(tabcrtlFleet.TabPages.Count - 1)
                        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(pgTabIndex).Select()
                tabcrtlFleet.TabPages(pgTabIndex).ForeColor = Color.AliceBlue
                tabcrtlFleet.TabPages(pgTabIndex).Text = "Change" & pgTabIndex
                'Creating Labels
                For labelCreate = 1 To 9
                    tabcrtlFleet.TabPages(pgTabIndex).Controls.Add(newLabel)

                    With newLabel
                        .Name = item.ToString  & (pgTabIndex + 1)
                        .Text = item.ToString
                        .Size = New Drawing.Size(ctrlsSizeXArray(Arrayindex), ctrlsSizeYArray(Arrayindex))
                        .Location = New Point(ctrlsLocXArray(Arrayindex), ctrlsLocYArray(Arrayindex))
                        .Visible = True
                    End With

                    'Creating LinkLabel
                    For LinkLabelCreate = 1 To 1
                        tabcrtlFleet.TabPages(pgTabIndex).Controls.Add(newLinklabel)
                        With newLinklabel
                            .Name = item.ToString  & (pgTabIndex + 1)
                            .Text = item.ToString
                            .Size = New Drawing.Size(ctrlsSizeXArray(Arrayindex), ctrlsSizeYArray(Arrayindex))
                            .Location = New Point(ctrlsLocXArray(Arrayindex), ctrlsLocYArray(Arrayindex))
                        End With
                    Next

                    'Creating DataGridView
                    For DataGridCreation = 1 To 2
                        tabcrtlFleet.TabPages(pgTabIndex).Controls.Add(newDataGrid)
                        With newDataGrid
                            .Name = item.ToString  & (pgTabIndex + 1)
                            .Text = item.ToString
                            .Size = New Drawing.Size(ctrlsSizeXArray(Arrayindex), ctrlsSizeYArray(Arrayindex))
                            .Location = New Point(ctrlsLocXArray(Arrayindex), ctrlsLocYArray(Arrayindex))
                        End With
                    Next
                    'Creating Textboxes
                    For TextboxCreate = 1 To 9
                        tabcrtlFleet.TabPages(pgTabIndex).Controls.Add(newTextBox)
                        With newTextBox
                            .Name = item.ToString  & (pgTabIndex + 1)
                            .Text = item.ToString
                            .Size = New Drawing.Size(ctrlsSizeXArray(Arrayindex), ctrlsSizeYArray(Arrayindex))
                            .Location = New Point(ctrlsLocXArray(Arrayindex), ctrlsLocYArray(Arrayindex))
                        End With
                    Next
                    'Creating Button
                    For buttonCreate = 1 To 1
                        tabcrtlFleet.TabPages(pgTabIndex).Controls.Add(newButton)
                        With newButton
                            .Name = item.ToString  & (pgTabIndex + 1)
                            .Text = item.ToString
                            .Size = New Drawing.Size(ctrlsSizeXArray(Arrayindex), ctrlsSizeYArray(Arrayindex))
                            .Location = New Point(ctrlsLocXArray(Arrayindex), ctrlsLocYArray(Arrayindex))
                        End With
                    Next
                Next
            Next


        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message & " " & ex.StackTrace)
        End Try
    End Sub

TabPages-created-at-run-time.JPGTabPages-created-at-run-time2.JPG
0
mwells08
Asked:
mwells08
  • 4
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why do it this way at all?  Add all those controls in your TabPage to a UserControl, then create instances of that UserControl at run-time and add it to the TabPage.  This way you'll be able to design it properly with the IDE and you'll be able to add code to everything as you normally would.
0
 
mwells08Author Commented:
Thank you Mike for replying. I am afraid I am not familiar with the way your are proposing; do you know of an example code I can see?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
At first view, it looks as if all your controls have the same Location and Size.

You use the same code for all the controls, but (Arrayindex) does not change.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You'd click on Project --> "Add User Control", give it a Name ("AircraftData?") , and hit the "Add" Button.

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:
Example UserControl "AircraftData"
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

Open in new window

The AircraftData UserControl added to a TabPage
0
 
mwells08Author Commented:
Thanks Jacques for your reply.

Mike thank you so much for the example you gave me. I'll give it a try!
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
A UserControl is useful if you reuse it in many places. If it is only to fill one TabPage, it is overkill, specially if you have to create your interface dynamically.

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.
0
 
mwells08Author Commented:
Thank you Jacques,
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.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Looking carefully at your code, I see that you do not recreate a new control in each loop. You are thus continually adding the same control 9 times, so all you do is move it around. I also repeat what I said before: you do not change the Location, so the controls would simply pile up one over the other.

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.
0
 
mwells08Author Commented:
Mike and Jacques thank you very much for your help. I was able to create the tabs and now I can see the controls.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now