r/visualbasic • VB.Net Advanced • May 12 '15

VB.NET Help TableLayoutPanel woes

I'm using, or at least attempting to use a table layout panel on a form. The panel is being created dynamically at runtime as the contents of a tab. The tabs can be added and deleted by the user at runtime.

My issue is the contents of the panel are not showing up on the tab. They seem to be behind it and actually not bound by the borders of the tab.

The code...

    Private Sub PopulateTab(ByVal iTabPage As Integer)
    Dim txt As Label
    Dim grid As DataGridView
    Dim label As Label
    Dim iCol As Integer, iRow As Integer
    Dim panel As TableLayoutPanel

    'create the panel for layout
    panel = New TableLayoutPanel()
    panel.Name = "pnlSystemBodies" & iTabPage.ToString()
    panel.Location = New Point(3, 32)
    panel.Size = New Size(816, 250)

    panel.ColumnCount = 2
    panel.RowCount = 2
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 75.0F))
    panel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0F))
    panel.RowStyles.Add(New RowStyle(SizeType.Absolute, 20.0F))
    panel.RowStyles.Add(New RowStyle(SizeType.Percent, 100.0F))

    panel.Visible = True
    panel.Dock = DockStyle.Fill
    panel.Parent = tabSysBodies.TabPages(iTabPage)
    panel.Anchor = AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right

    'create a label for the primary type
    txt = New Label
    txt.Name = "txtPrimary" & iTabPage
    txt.Location = New Point(58, 38)
    txt.Parent = panel

    genSystem.primary(iTabPage).stellarAge = aiSysGen.GetStellarAge()
    genSystem.primary(iTabPage).spectrum = aiSysGen.GetSpectrum(genSystem.primary(iTabPage).stellarAge, iTabPage)

    txt.Text = genSystem.primary(iTabPage).spectrum
    txt.AutoSize = True

    'create a data grid for the orbital information
    grid = New DataGridView
    grid.Name = "grdSystem" & iTabPage
    grid.Location = New Point(78, 38)

    grid.Size = New Size(612, 215)
    grid.Visible = True
    grid.Parent = panel

    'add the controls to the layout grid
    panel.Controls.Add(txt, 0, 0)
    panel.Controls.Add(grid, 0, 1)

    With grid.Columns
        .Add("Orbit", "Orbit")
        .Add("Distance", "Distance")
        .Add("PType", "Type")
        .Add("Dia", "Diameter")
        .Add("Size", "Size")
        .Add("Gravity", "Gravity")
        .Add("Atmo", "Atmosphere")
        .Add("AtmoComp", "Atmospheric Composition")
        .Add("OI", "Organic Index")
        .Add("MI", "Mineral Index")
        .Add("Abundance", "Mineral Abundance")
        .Add("EIV", "Exploitation Index")
        .Add("Geo", "Geology")
        .Add("Hydro", "Hydrosphere")
        .Add("HydroPercent", "Hydro %")
        .Add("Bio", "Biosphere")
        .Add("Temp", "Temperature")

    End With

    Controls.Add(panel)

End Sub

Some of the code is experimental with me trying to figure out how to make the bloody thing work the way I want it to work. As it is, the code is also not complete. There are more controls to add it, but I see no point in doing that until I get the label and grid properly positioned.

Any one have any ideas on how to rectify this? I'll attach a screenshot in a moment.

Screenshot of the effect. https://www.dropbox.com/s/xgvyt03ql11hrb1/custom_system.PNG?dl=0

5 Upvotes

5 comments sorted by

2

u/hdsrob May 12 '15

First a suggestion:

If the dynamic tabs are all identical (but with different data), then I'd just create a control that represents a single tab, and then create a new instance of the control, populate the data, and add the single control to the tab.

On the current code:

Instead of using .parent, switch to adding the the panel to the actual tab page that it's on (this worked in a quick dirty test).

 tabSysBodies.TabPages(iTabPage).controls.add(panel)

2

u/icehawke VB.Net Advanced May 13 '15

That seems to work, though I did notice something else I'll need to fix. Logic-wise, not layout-wise.

Rolling the custom control sounds like a good idea too, though I think the last time I did that was in VB5 or 6.

1

u/Bonejob VB Guru May 12 '15

Please put a bullet in my head. The table layout control should have been shot and buried in .NET 2.

I did have an article bookmarked from a guy Hassan Mokdad - Designing the Layout of Windows Forms using a TableLayoutPanel, with auto-expand panels

he goes into details of the internal workings of the control so maybe you can pick out what is going on with your code.

I cant see what is causing it from your code. Do you have a git repo or a zip I can snag?

2

u/icehawke VB.Net Advanced May 12 '15

You should be able to grab it from here. https://www.dropbox.com/sh/71jvlellemduuak/AADc5WT3FwpQ68vL18HfgAnAa?dl=0

This code is in AICore_System, frmCustomSystem. To access that code runtime, you need to create a load game and type CSYS in the text box in the upper right corner. Then clicking "Add" triggers the code.

edit: I saw that article this afternoon while I was digging around.

2

u/JakDrako May 13 '15

Tackling a TableLayoutPanel question? You, my friend, are a courageous man.