Tablelayoutpanel can arrange the controls in the cells by defining the column and row number. We can add controls by defining the column and row Number.
Add a TableLayoutPanel control in the form and name it as “TablelayoutPnl”.
Dim IDLbl As New Label IDLbl.Name = "ID" IDLbl.Size = New System.Drawing.Size(10, 17) IDLbl.Text = "Test" Dim IDpos As New TableLayoutPanelCellPosition(0, 0) TablelayoutPnl.SetCellPosition(IDLbl, IDpos) TablelayoutPnl.Controls.Add(IDLbl) Dim NameTxt As New TextBox NameTxt.Size = New System.Drawing.Size(100, 17) NameTxt.Text = "Test Data" Dim NamePos As New TableLayoutPanelCellPosition(1, 0) TablelayoutPnl.SetCellPosition(NameTxt, NamePos) TablelayoutPnl.Controls.Add(NameTxt)
Here one label and text box will be created in the first row in the first and second cell respectively.TableLayoutPanelCellPosition takes two parameters column and row. we can increase the row number in a for…loop if we want to create more number of rows.
Fetching controls in TableLayoutPanel
Dim ctrlInLastRow(TablelayoutPnl.ColumnCount - 1) As Control
Dim IDLbl As New Label
Dim NameTxt As New TextBox
Dim IDStr As String = String.Empty
Dim NameStr As String = String.Empty
For Each ctrl As Control In TablelayoutPnl.Controls
Dim cell As TableLayoutPanelCellPosition = TablelayoutPnl.GetCellPosition(ctrl)
If cell.Row = TablelayoutPnl.GetRow(ctrl) Then
ctrlInLastRow(cell.Column) = ctrl
Select Case cell.Column
Case 0
IDLbl = CType(ctrl, Label)
IDStr = IDLbl.Text
Case 2
NameTxt= CType(ctrl, TextBox)
NameStr = NameTxt.Text
End Select
End If
Next
Here we can get the created label and text box’s values according to the cell in the TablelayoutPanel.