【发布时间】:2009-07-29 18:47:23
【问题描述】:
我有一个控件,我需要在设计时限制它可以包含的子控件的类型(将新控件拖到表单设计器上的现有控件上)。我试图通过覆盖 OnControlAdded 事件来做到这一点:
Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
MyBase.OnControlAdded(e)
If e.Control.GetType() IsNot GetType(ExpandablePanel) Then
MsgBox("You can only add the ExpandablePanel control to the TaskPane.", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "TaskPane")
Controls.Remove(e.Control)
End If
End Sub
这似乎可行,但是在删除控件后我立即从 Visual Studio 收到一条错误消息:
'child' 不是这个父级的子控件。
这是什么意思?我怎样才能做到这一点而不发生错误?
【问题讨论】: