TableLayoutPanel不会删除控件

时间:2013-11-18 17:22:37

标签: vb.net tablelayoutpanel

添加正常

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

MyButton.MyButton在UserControl的类中声明为

Friend WithEvents m_btnMy As MyButton.MyButton

但删除功能不是

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

在单步执行RemoveColumnFromTableLayout()时,我注意到GetControlFromPosition正在返回“Nothing”。我从一个2x2矩阵开始,添加一列后,我正确地有一个带按钮的2x3矩阵。删除后,我错误地使用相同按钮组的3x2矩阵。在我意识到GetControl返回“Nothing”之前,我尝试在控件上使用dispose。

User Control output

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

一些一般性说明:

  • 您必须显式增加/减少ColumnCount() 属性。
  • ColumnStyle()Width属性不需要是实际的 计算百分比。只需使所有列都具有相同即可 值。我已经使用了第一列中的任何值。接着就,随即 请记住,当列没有时,您不必更改任何宽度 删除,因为它们已经是相同的值。
  • 你有一个"关闭一个" Add()的Column值中的错误 例程。
  • 在Remove()例程中,您的行/列参数为已反转 GetControlFromPosition()来电。

以下是修改后的代码:

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