vb.net 运行时的 TableLayoutPanel 行和列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17822822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:21:59  来源:igfitidea点击:

TableLayoutPanel rows & columns at runtime

.netvb.netdynamicruntimetablelayoutpanel

提问by 4ntibala

im trying to build a usercontrol which contains a tablelayoutpanel. in this panel i need to dynamically add 3 columns with each having different a width and 5 rows which all shell have the same height (20% of the tablelayoutpanel's height).

我正在尝试构建一个包含 tablelayoutpanel 的用户控件。在此面板中,我需要动态添加 3 列,每列具有不同的宽度和 5 行,所有外壳都具有相同的高度(tablelayoutpanel 高度的 20%)。

column1 should have an absolute width of 20, column2 depending a width on its content (a textbox with .dock = fill) column3 a width of 30.

column1 的绝对宽度应为 20,column2 的宽度取决于其内容(带有 .dock = fill 的文本框) column3 的宽度为 30。

my code:

我的代码:

Private Sub BuildGUI()
    If Rows > 0 Then

        tlp.Controls.Clear()
        tlp.ColumnStyles.Clear()
        tlp.RowStyles.Clear()


        If Style = Styles.Adding Then
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 30))
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Autosize))
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
            tlp.ColumnCount = 3

            tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize, 20%))
            tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize, 20%))
            tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize, 20%))
            tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize, 20%))
            tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize, 20%))
            tlp.RowCount = Rows

            For i = 0 To Rows - 1
                Dim L As New Label
                Dim T As New TextBox
                Dim C As New CheckBox

                With L
                    .BackColor = Color.Aqua
                    '.Dock = DockStyle.Fill
                    .Visible = True
                    .BorderStyle = Windows.Forms.BorderStyle.FixedSingle
                    .Font = New Font("Microsoft Sans Serif", 11, FontStyle.Bold)
                End With
                tlp.Controls.Add(L, 0, i)

                With T
                    .BackColor = Color.Beige
                    .Visible = True
                    .Multiline = True
                    .ScrollBars = ScrollBars.Vertical
                    .Dock = DockStyle.Fill
                End With
                tlp.Controls.Add(T, 1, i)

                With C
                    .Visible = True
                    .BackColor = Color.Brown
                End With
                tlp.Controls.Add(C, 2, i)

            Next
        Else

        End If

    End If

End Sub

结束子

Styles & Rows are properties of the Usercontrol.

样式和行是用户控件的属性。

but the result is never as i want it to be. any ideas?

但结果永远不是我想要的。有任何想法吗?

回答by 4ntibala

for anybody struggling with the same:

对于任何有同样问题的人:

Private Sub BuildGUI()
    If Rows > 0 Then

        tlp.Controls.Clear()
        tlp.ColumnStyles.Clear()
        tlp.RowStyles.Clear()

        If Style = Styles.Adding Then
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 30))
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 80%))
            tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 50))
            tlp.ColumnCount = 3


            For i = 0 To Rows - 1
                tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100 / Rows))

                Dim L As New Label
                Dim T As New TextBox
                Dim C As New CheckBox

                With L
                    .Text = Chr(65 + i)
                    .TextAlign = ContentAlignment.MiddleCenter
                    .Visible = True
                    .Font = New Font("Microsoft Sans Serif", 11, FontStyle.Bold)
                    .Dock = DockStyle.Fill
                End With
                tlp.Controls.Add(L, 0, i)

                With T
                    .Name = "txt" & Chr(65 + i)
                    .Visible = True
                    .Multiline = True
                    .ScrollBars = ScrollBars.Vertical
                    .Dock = DockStyle.Fill
                End With
                tlp.Controls.Add(T, 1, i)

                With C
                    .Name = "chk" & Chr(65 + i)
                    .CheckAlign = ContentAlignment.MiddleCenter
                    .Visible = True
                    .BackColor = Color.LightGray
                    .Dock = DockStyle.Fill
                End With
                tlp.Controls.Add(C, 2, i)

            Next

        Else

        End If

    End If
End Sub