TableLayoutPanel rows & columns at runtime
14,310
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
Author by
4ntibala
Updated on June 05, 2022Comments
-
4ntibala almost 2 years
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?