Share via


How to insert or delete specific row in tablelayoutpanel?

Question

Wednesday, March 16, 2011 4:31 PM

Hi, I asked on previous post similar question but didn't get the answer. So, I am asking how to let say insert row between row8 and row9 in runtime by clicking a button, and how to let say delete row9 and the textbox in it? I am new at this, so any help is great :-). Please!!!

 

All replies (1)

Thursday, March 17, 2011 1:00 AM âś…Answered

Give this a try.

 

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click

      Dim c As Control
      Dim tempHolding As New List(Of HoldingCell)
      Dim cell As HoldingCell
      Dim deleteIndex As Int32 = 3 'the 4th row, zero based

      With TableLayoutPanel1
         'Delete all controls on selected row
         For col As Int32 = 0 To .ColumnCount - 1
            c = .GetControlFromPosition(column:=col, row:=deleteIndex)
            If c IsNot Nothing Then
               .Controls.RemoveByKey(c.Name) 'remove it from the controls collection
               c.Dispose() 'get rid of it
            End If
         Next col

         'Temporarly Store the Positions
         For row As Int32 = deleteIndex + 1 To TableLayoutPanel1.RowCount - 1
            For col As Int32 = 0 To TableLayoutPanel1.ColumnCount - 1
               cell = New HoldingCell
               cell.cntrl = .GetControlFromPosition(col, row)
               'setup position for restore = current row -1
               cell.pos = New TableLayoutPanelCellPosition(col, row - 1)
               tempHolding.Add(cell)
            Next col
         Next row

         'delete the row
         .RowStyles.RemoveAt(index:=deleteIndex) 'deletes the style only
         .RowCount -= 1

         'adjust control positions
         For Each cell In tempHolding
            If cell.cntrl IsNot Nothing Then
               .SetCellPosition(cell.cntrl, cell.pos)
            End If
         Next cell
      End With
      tempHolding = Nothing

   End Sub

   Private Sub Button3_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button3.Click

      Dim insertPoint As Int32 = 0 ' insert as 1st row

      Dim tempHolding As New List(Of HoldingCell)
      Dim cell As HoldingCell
      With TableLayoutPanel1
         For row As Int32 = insertPoint To TableLayoutPanel1.RowCount - 1
            For col As Int32 = 0 To TableLayoutPanel1.ColumnCount - 1
               cell = New HoldingCell
               cell.cntrl = .GetControlFromPosition(col, row)
               'setup position for restore = current row + 1
               cell.pos = New TableLayoutPanelCellPosition(col, row + 1)
               tempHolding.Add(cell)
            Next col
         Next row

         'insert new row
         .RowStyles.Insert(insertPoint, New RowStyle(SizeType.Absolute, 30.0!))
         .RowCount += 1

         'adjust control positions
         For Each cell In tempHolding
            If cell.cntrl IsNot Nothing Then
               .SetCellPosition(cell.cntrl, cell.pos)
            End If
         Next cell
      End With
      tempHolding = Nothing
   End Sub

   Structure HoldingCell
      Dim cntrl As Control
      Dim pos As TableLayoutPanelCellPosition
   End Structure

End Class