How do I remove a control if not a specific type?

Posted by joek1975 on Stack Overflow See other posts from Stack Overflow or by joek1975
Published on 2009-07-29T18:47:23Z Indexed on 2010/04/19 9:03 UTC
Read the original article Hit count: 162

Filed under:
|

I have a control that I need to restrict the types of child controls that it can contain during design time (dragging a new control onto an existing control on the form designer). I tried to do this by overriding the OnControlAdded event:

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

This seems to work, however I get an error message from Visual Studio immediately after the control is removed:

'child' is not a child control of this parent.

What does this mean? How can I accomplish this without errors occurring?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about winforms