[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-13
3
Medium Priority
?
194 Views
Last Modified: 2010-04-24
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.
0
Comment
Question by:agilitycns
2 Comments
 
LVL 1

Author Comment

by:agilitycns
ID: 12576067
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12888535
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question