8

I generate controls for a TableLayoutPanel dynamically. I have a delete button in each row. When I click that, that row has to be removed.

    Dim removeBtn As New Button
    AddHandler removeBtn.Click, AddressOf DeleteRow
    tlp.Controls.Add(removeBtn, 5, rowCount)

I have not shown the code to add text boxes which are similar to above. I can get the row number of the clicked button. Using this, how to remove all controls from this row.

Private Sub DeleteRow(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim currentRow As Integer = CType(CType(sender, Button).Parent, TableLayoutPanel).GetRow(CType(sender, Button))
   'Using this currentRow, how to delete this Row
End Sub

3 Answers 3

21

Basically you have to:

  • Get the list of controls from that row and delete them from the TLP
  • Remove the corresponding row style from the TLP
  • Set the new row index for every control in every row after the deleted one
  • Decrement the RowCount

Here is the VB.NET code to do the same.

Public Sub RemoveRow(ByRef panel As TableLayoutPanel, ByRef rowIndex As Integer)

    panel.RowStyles.RemoveAt(rowIndex)
    Dim columnIndex As Integer
    For columnIndex = 0 To panel.ColumnCount - 1
        Dim Control As Control = panel.GetControlFromPosition(columnIndex, rowIndex)
        panel.Controls.Remove(Control)
    Next
    Dim i As Integer
    For i = rowIndex + 1 To panel.RowCount - 1
        columnIndex = 0
        For columnIndex = 0 To panel.ColumnCount - 1
            Dim control As Control = panel.GetControlFromPosition(columnIndex, i)
            panel.SetRow(control, i - 1)
        Next
    Next
    panel.RowCount -= 1
End Sub

Here is a C# extension method that will do this for you.

public static void RemoveRow(this TableLayoutPanel panel, int rowIndex)
{
    panel.RowStyles.RemoveAt(rowIndex);

    for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
    {
        var control = panel.GetControlFromPosition(columnIndex, rowIndex);
        panel.Controls.Remove(control);
    }

    for (int i = rowIndex + 1; i < panel.RowCount; i++)
    {
        for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
        {
            var control = panel.GetControlFromPosition(columnIndex, i);
            panel.SetRow(control, i - 1);
        }
    }

    panel.RowCount--;
}
2
  • 1
    @emaillenin: Thanks for the translation to VB.NET Commented Jun 2, 2011 at 15:27
  • Why does ones have to remove the RowStyle?
    – sammarcow
    Commented Feb 9, 2012 at 16:08
3

In addition to Johann and emaillenin's answers you should change the following line

    panel.SetRow(control, i - 1);

To this

    if (control != null) panel.SetRow(control, i - 1);

Empty fields and also spanned controls will throw an error here if theres no check for null.

-7

Why this much hardwork... Use tableLayoutpanel1.Controls.Clear()

This will clear the contents of table panel

1
  • 5
    Which is not what the OP was asking to do. Commented Feb 12, 2013 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.