在 TableLayoutPanel 中插入行不起作用

Insert row in TableLayoutPanel not working

我动态创建了一个 TableLayoutPanel,我向其中添加了 12 行并添加了一些控件。我在 TLP 的末尾添加行没有问题。

现在我需要在 TLP 的第 2 行插入一行,以便将现有控件下移。我的代码不起作用。它不会添加新行。它将标签添加到现有行并将该单元格中的控件推送到下一个空单元格。

        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)

不确定是否有更好的方法...

这会降低所有内容,使您的新控件 space:

    ' 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)