none
How to insert or delete specific row in tablelayoutpanel?

    Question

  • 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!!!

     

    Wednesday, March 16, 2011 4:31 PM

Answers

  • 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
    Thursday, March 17, 2011 1:00 AM

All replies

  • 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
    Thursday, March 17, 2011 1:00 AM
  • Thak you TnTin and sorry for a late reply - I wasn't near computer for a last few days. As I am a beginner to VB I have to work with your lines of code do understand them. 

    Thank you once again.

    Sunday, March 20, 2011 2:54 PM
  • If you have any questions, please feel free to ask.  I'll do my best to answer them.  Currently, I'm not getting email notifications from the forum (again, :-/), so don't be offended if I don't reply in a short time period.
    Sunday, March 20, 2011 4:34 PM