我有一个动态创建的TableLayoutPanel,我已经添加了12行并添加了一些控件。我没有问题在TLP的末尾添加行。
现在我需要在第2行的TLP中插入一行,以便将现有控件向下移动。我的代码不起作用。它不会添加新行。它将标签添加到现有行中,并将该单元格中的控件推送到下一个空单元格。
Checkrow = 2
Layout_SidePanel.RowCount += 1
Layout_SidePanel.RowStyles.Insert(CheckRow, New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40))
Layout_SidePanel.Refresh()
Dim lbl As New Label
lbl.Margin = New Padding(6, 6, 3, 3)
lbl.Text = " "
Layout_SidePanel.Controls.Add(lbl, 1, CheckRow)
答案 0 :(得分:1)
不确定是否有更好的方法...
这会降低一切,为你的新控制创造空间:
' Row to insert at:
Checkrow = 2
' Add the Row:
Layout_SidePanel.RowCount += 1
Layout_SidePanel.RowStyles.Insert(Checkrow, New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40))
' Shift everything down:
For r As Integer = Layout_SidePanel.RowCount - 1 To Checkrow + 1 Step -1
For c As Integer = Layout_SidePanel.ColumnCount - 1 To 0 Step -1
Dim ctl As Control = Layout_SidePanel.GetControlFromPosition(c, r - 1)
If Not IsNothing(ctl) Then
Layout_SidePanel.SetCellPosition(ctl, New TableLayoutPanelCellPosition(c, r))
End If
Next
Next
' Insert the new control:
Dim lbl As New Label
lbl.Margin = New Padding(6, 6, 3, 3)
lbl.Text = " "
Layout_SidePanel.Controls.Add(lbl, 1, Checkrow)