添加正常
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。
感谢您的帮助。
答案 0 :(得分:2)
一些一般性说明:
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