Solved

controls created dynamically in tabpage are not showing

Posted on 2014-12-31
9
129 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

20 Experts available now in Live!

Get 1:1 Help Now