garaber
garaber

Reputation: 232

TableLayoutPanel isn't removing Controls

The add is working correctly

Private Sub AddColumnToTableLayout()
    Me.m_TblLyBtnHost.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, (100 / m_TblLyBtnHost.ColumnCount)))
    For iColumnStyle As Integer = 0 To Me.m_TblLyBtnHost.ColumnStyles.Count - 1
        Me.m_TblLyBtnHost.ColumnStyles.Item(iColumnStyle).SizeType = SizeType.Percent
        Me.m_TblLyBtnHost.ColumnStyles.Item(iColumnStyle).Width = 100 / Me.m_TblLyBtnHost.ColumnCount
    Next

    'For iColumns As Integer = 0 To m_TblLyBtnHost.ColumnCount - 1
    '    Me.m_TblLyBtnHost.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, (100 / m_TblLyBtnHost.ColumnCount)))
    'Next
    'Insert buttons for each of the new row ends
    For iRowIndex As Integer = 0 To Me.m_TblLyBtnHost.RowCount - 1
        m_btnMy = New MyButton.MyButton
        Me.SetDefaultsOnMyButtonMyButton(m_btnMy, Me.m_TblLyBtnHost.ColumnCount, iRowIndex)
        Me.m_TblLyBtnHost.Controls.Add(m_btnMy, Me.m_TblLyBtnHost.ColumnCount, iRowIndex)
    Next

End Sub

The MyButton.MyButton is declared in the the class for the UserControl as

Friend WithEvents m_btnMy As MyButton.MyButton

The Remove function however isn't

Private Sub RemoveColumnFromTableLayout()
    For iRowIndex As Integer = 0 To Me.m_TblLyBtnHost.RowCount - 1
        Dim Ctrl As MyButton.MyButton = Me.m_TblLyBtnHost.GetControlFromPosition(iRowIndex, Me.m_TblLyBtnHost.ColumnCount - 1)
        Me.m_TblLyBtnHost.Controls.Remove(Ctrl)
    Next
    Me.m_TblLyBtnHost.ColumnStyles.RemoveAt(Me.m_TblLyBtnHost.ColumnCount - 1)
    For iColumnIndex As Integer = 0 To Me.m_TblLyBtnHost.ColumnStyles.Count - 1
        Me.m_TblLyBtnHost.ColumnStyles.Item(iColumnIndex).Width = 100 / Me.m_TblLyBtnHost.ColumnCount
    Next
End Sub

In stepping through RemoveColumnFromTableLayout() I noticed that GetControlFromPosition is returning "Nothing". I started with a 2x2 matrix and after adding a column I correctly have a 2x3 matrix with button. After a remove I incorrect have a 3x2 matrix of the same button set. I tried using a dispose on the control before I realized that the GetControl was returning "Nothing".

User Control output

Thanks for any help.

Upvotes: 3

Views: 1495

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39132

Some general notes:

  • You have to explicitly increment/decrement the ColumnCount() property.
  • The ColumnStyle() Width property does NOT need to be an actual computed percentage. Simply make all the columns have the same value. I've used whatever value is in the first column. With that in mind, you don't have to change any of the Widths when a column is removed, since they are all the same value already.
  • You had an "off by one" error in the Column value for the Add() routine.
  • In the Remove() routine, your row/col parameters were Reversed in the GetControlFromPosition() call.

Here's the revised code:

Private Sub AddColumnToTableLayout()
    Me.m_TblLyBtnHost.ColumnCount = Me.m_TblLyBtnHost.ColumnCount + 1
    Me.m_TblLyBtnHost.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, Me.m_TblLyBtnHost.ColumnStyles(0).Width))

    'Insert buttons for each of the new row ends
    For iRowIndex As Integer = 0 To Me.m_TblLyBtnHost.RowCount - 1
        m_btnMy = New MyButton.MyButton
        Me.SetDefaultsOnMyButtonMyButton(m_btnMy, Me.m_TblLyBtnHost.ColumnCount - 1, iRowIndex)
        Me.m_TblLyBtnHost.Controls.Add(m_btnMy, Me.m_TblLyBtnHost.ColumnCount - 1, iRowIndex)
    Next

End Sub

Private Sub RemoveColumnFromTableLayout()
    For iRowIndex As Integer = 0 To Me.m_TblLyBtnHost.RowCount - 1
        Me.m_TblLyBtnHost.GetControlFromPosition(Me.m_TblLyBtnHost.ColumnCount - 1, iRowIndex).Dispose()
    Next
    Me.m_TblLyBtnHost.ColumnStyles.RemoveAt(Me.m_TblLyBtnHost.ColumnCount - 1)
    Me.m_TblLyBtnHost.ColumnCount = Me.m_TblLyBtnHost.ColumnCount - 1
End Sub

Upvotes: 3

Related Questions