trap an Add/Remove BEFORE a control is removed from a Control.ControlCollection

I’m trying to figure out how I can raise an event before a control is added to or removed from a Control.ControlCollection.  I just can’t seem to get it.  Any information would be greatly appreciated.
LVL 1
agilitycnsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

agilitycnsAuthor Commented:
well after trying everything i could think of and searching everywhere on the net I posted my question here, and of course... i immediately found the solution (http://www.martnet.com/~jfosler/articles/OverridingControlCollection.htm).  But since it was so difficult to find i'll post my code here incase the website i found the answer at doesn't survive or moves.  I hope this helps somebody else.

' the custom control collection's consumer
Public Class MyCustomPanel
    Inherits System.Windows.Forms.Panel

    Dim WithEvents mControls As MyCustomControlCollection

    Protected Overrides Function CreateControlsInstance() As Control.ControlCollection
        If mControls Is Nothing Then
            mControls = New MyCustomControlCollection(Me)
        End If
        Return mControls
    End Function

    Public Shadows ReadOnly Property Controls() As ControlCollection
        Get
            If mControls Is Nothing Then
                mControls = New MyCustomControlCollection(Me)
            End If
            Return mControls
        End Get
    End Property

    Public Sub New()
        MyBase.New()
        CreateControl() ' without this the control won't draw it's child controls during run-time
    End Sub

    Private Sub MyCustomPanel_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
        Dim f As New Font(CType(sender, Control).Font.FontFamily, 8)
        CType(sender, Control).Font = f
    End Sub

    Private Sub mControls_BeforeRemove(ByVal value As System.Windows.Forms.Control, ByRef Cancel As Boolean) Handles mControls.BeforeRemove
        If MsgBox("remove object?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Cancel = False
        Else
            Cancel = True
        End If
    End Sub

    Private Sub mControls_RemoveCanceled(ByVal value As System.Windows.Forms.Control) Handles mControls.RemoveCanceled
        MsgBox("the object was not removed")
    End Sub
End Class

' the custom control
Public Class MyCustomControlCollection
    Inherits Control.ControlCollection

    Public Event BeforeAdd(ByVal value As System.Windows.Forms.Control, ByRef c As Boolean)
    Public Event AddCanceled(ByVal value As System.Windows.Forms.Control)

    Public Event BeforeAddRange(ByVal controls() As System.Windows.Forms.Control, ByRef c As Boolean)
    Public Event AddRangeCanceled(ByVal controls() As System.Windows.Forms.Control)

    Public Event BeforeClear(ByRef Cancel As Boolean)
    Public Event ClearCanceled()

    Public Event BeforeRemove(ByVal value As System.Windows.Forms.Control, ByRef Cancel As Boolean)
    Public Event RemoveCanceled(ByVal value As System.Windows.Forms.Control)

    Public Sub New(ByVal owner As System.Windows.Forms.Control)
        MyBase.New(owner)
    End Sub

    Public Overrides Sub Add(ByVal value As System.Windows.Forms.Control)
        Dim c As Boolean
        c = False
        RaiseEvent BeforeAdd(value, c)
        If Not c Then
            MyBase.Add(value)
        Else
            RaiseEvent AddCanceled(value)
        End If
    End Sub

    Public Overrides Sub AddRange(ByVal controls() As System.Windows.Forms.Control)
        Dim c As Boolean
        c = False
        RaiseEvent BeforeAddRange(controls, c)
        If Not c Then
            MyBase.AddRange(controls)
        Else
            RaiseEvent AddRangeCanceled(controls)
        End If
    End Sub

    Public Overrides Sub Clear()
        Dim c As Boolean
        c = False
        RaiseEvent BeforeClear(c)
        If Not c Then
            MyBase.Clear()
        Else
            RaiseEvent ClearCanceled()
        End If
    End Sub

    Public Overrides Sub Remove(ByVal value As System.Windows.Forms.Control)
        Dim c As Boolean
        c = False
        RaiseEvent BeforeRemove(value, c)
        If Not c Then
            MyBase.Remove(value)
        Else
            RaiseEvent RemoveCanceled(value)
        End If
    End Sub
End Class
0
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.