0
votes

I have a dynamically created a TableLayoutPanel to which I have added 12 rows and added some controls. I had no problem adding rows to the end of the TLP.

Now I need to insert a row into the TLP at row 2 so it shifts the existing controls down. My code doesn't work. It doesn't add a new row. It adds the label into the existing row and pushes the control that was in that cell to the next empty cell.

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

1 Answers

1
votes

Not sure if there is a better way to do it...

This shifts everything down making space for your new control:

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