TableLayoutPanel rows & columns at runtime

走远了吗. 提交于 2020-01-03 00:55:21

问题


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).

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.

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?


回答1:


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


来源:https://stackoverflow.com/questions/17822822/tablelayoutpanel-rows-columns-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!