0

Hi having some issues with some nested tablelayoutpanels in a custom control I have a tablelayoutpanel in a tablelayoutpanel in a tablelayoutpanel Crazy i know but it keeps it uniform and ordered

The custom control consists of 2 tablelayoutpanels that is placed in a table layout panel on a form and in a preview area of my main form.

Having them set at design time works fine but dynamically adding / removing the topmost ones with 1 row 2 columns into the one below it that has 1 column and x rows only Seems to break the autosizing behavior I'm chasing.
So I want the cells & rows to resize automatically based on the contents in this case labels but still remain in a neat ordered layout

There's no docking anywhere in the hierarchy of controls just anchors here and there Here's my code to add the tablelayoutpanels below
"https://i.stack.imgur.com/vRfhE.png"



Private Sub AddControl(ByRef Counter As Counter)
        Dim Gpanel As New TableLayoutPanel
        Dim tlabel As New Label
        Dim clabel As New Label
        Dim pad As Integer = Counter.Cpad

        TLPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
        Gpanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
        Gpanel.AutoSize = True
        Gpanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows
        Gpanel.BorderStyle = BorderStyle.FixedSingle
        Gpanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
        TLPanel.BorderStyle = BorderStyle.FixedSingle
        Gpanel.Dock = DockStyle.None
        Gpanel.Padding = New Padding(0)
        Gpanel.Anchor = AnchorStyles.Top Or AnchorStyles.Left
        Gpanel.BackColor = Color.Transparent
        Gpanel.RowCount = 1
        Gpanel.ColumnCount = 2
        Gpanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
        Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
        Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
        tlabel.Dock = DockStyle.None
        clabel.Dock = DockStyle.None
        tlabel.GetPreferredSize(Size.Empty)
        clabel.GetPreferredSize(Size.Empty)
        tlabel.Text = Counter.Clabel
        clabel.Text = Counter.Ccount.ToString
        tlabel.Padding = New Padding(pad)
        clabel.Padding = New Padding(pad)
        tlabel.Anchor = AnchorStyles.None
        clabel.Anchor = AnchorStyles.None
        tlabel.ForeColor = Color.FromName(Counter.Clcolor)
        clabel.ForeColor = Color.FromName(Counter.Ccolor)
        Dim fontstyle As New FontStyle
        fontstyle = Counter.ClfontStyle
        tlabel.Font = New Font(Counter.Clfont, Counter.Clfontsize, fontstyle)
        fontstyle = Counter.CcfontStyle
        clabel.Font = New Font(Counter.Ccfont, Counter.Ccfontsize, fontstyle)
        Gpanel.Controls.Add(tlabel, 0, 0)
        Gpanel.Controls.Add(clabel, 1, 0)


        TLPanel.Controls.Add(Gpanel, 0, Counter.ID)

        TLPanel.RowCount += 1
        TLPanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
        Dim Styles As TableLayoutRowStyleCollection = TLPanel.RowStyles
        Dim Cstyles As TableLayoutColumnStyleCollection = Gpanel.ColumnStyles
        Gpanel.RowStyles.Item(0) = New RowStyle(SizeType.AutoSize)

        TLPanel.ColumnStyles.Item(0) = New ColumnStyle(SizeType.AutoSize)
        For i = 0 To Cstyles.Count - 1
            Cstyles.Item(i) = New ColumnStyle(SizeType.AutoSize)
        Next
        For i = 0 To Styles.Count - 1
            Styles.Item(i) = New RowStyle(SizeType.AutoSize)
        Next
        TLPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
        AutoSize = True
        AutoSizeMode = AutoSizeMode.GrowAndShrink

    End Sub
'
ASh
  • 34,632
  • 9
  • 60
  • 82
  • 1
    *Having them set at design time works fine* → So look at the designer-generated code and copy and use it for run-time generation. – Reza Aghaei Jan 19 '20 at 00:09
  • Read the notes here: [Remove Row inside TableLayoutPanel makes a layout problem](https://stackoverflow.com/a/55225457/7444103) and [here](https://stackoverflow.com/a/54565075/7444103). – Jimi Jan 19 '20 at 03:39
  • Hi Reza, Thanks but it was actually the first place I looked. But I did however forget to suspend the layout of each panel while making changes but still no luck. – Chris Cullen Jan 19 '20 at 07:33
  • Hi Jimi, Thanks for that These are not exactly the issues I'm having but similar any controls i add to the bottom most TLP dont autosize on the contents the first row cells behave as expected and will resize with the labels text and or font size but the added TLP in the new rows wont despite the rowstyle being the same and all of the other properties ill just have to create it all at runtime instead – Chris Cullen Jan 19 '20 at 08:36
  • Ah Ha I Got it I had forgotten to set the labels Autosize property to true So after removing all the unnecessary stuff i added in desperation and simplified with using statements it all works as expected now. I also deleted everything in the designer and then re did them because I have had similar issues with TLP's after using the designer **Thanks Guys** – Chris Cullen Jan 19 '20 at 17:30
  • TLP scrolling is bugged. You should try to encapsulate your TLP in a Panel like describe here : https://stackoverflow.com/questions/15620454/tablelayoutpanel-displays-vertical-scroll/25601129#25601129. – Bioukh Apr 27 '21 at 12:53
  • @Bioukh Thanks but there's no scrolling in my custom control my issue was caused by a simple missing Autosize property on a label in one of the cells. – Chris Cullen Apr 28 '21 at 17:11

0 Answers0