Solved

controls created dynamically in tabpage are not showing

Posted on 2014-12-31
9
130 Views
Last Modified: 2015-01-02
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
Comment
Question by:mwells08
  • 4
  • 3
  • 2
9 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40526050
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
 

Author Comment

by:mwells08
ID: 40526067
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
 
LVL 40
ID: 40526109
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40526122
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mwells08
ID: 40526172
Thanks Jacques for your reply.

Mike thank you so much for the example you gave me. I'll give it a try!
0
 
LVL 40
ID: 40526279
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
 

Author Comment

by:mwells08
ID: 40527907
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
 
LVL 40
ID: 40528002
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
 

Author Closing Comment

by:mwells08
ID: 40528093
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now