问题描述
im试图建立一个包含tablelayoutpanel的用户控件。在此面板中,我需要动态添加3列,每列具有不同的宽度和5行,所有行具有相同的高度(tablelayoutpanel高度的20%)。
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的绝对宽度应为20,
column2的宽度取决于其内容的宽度(带有.dock = fill的文本框)
column3的宽度应为30。
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.
我的代码:
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
结束子
样式和样式;行是Usercontrol的属性。
Styles & Rows are properties of the Usercontrol.
但结果永远不会像我想要的那样。有任何想法吗?
but the result is never as i want it to be. any ideas?
推荐答案
适用于任何挣扎相同的人:
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
这篇关于TableLayoutPanel行和运行时的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!