2

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

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

我的代码:

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

结束子

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

但结果从来都不是我想要的。有任何想法吗?

4

1 回答 1

7

对于任何与之苦苦挣扎的人:

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
于 2013-07-24T04:59:40.063 回答