Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a customized Collection which is sometimes used during Designtime.
The cutomized classes which I want to add (and also remove) to the classes have Events which should be collected in my Collection and also given to outside of this collection.

If I work with the Runtime-part everything works like I want because my Add- and my Remove-Methods could add and remove the Handlers.

During Designtime I supposed that the method OnInsertComplete is called when pressing the Add-Button of the Editor - this works indirectly.
Also I supposed that the method OnRemove or OnRemoveComplete is called when pressing the Remove-Button of the Editor - but this never happens.

So I thought about a work-around which catches the method-delegate of the Click-Event of the Remove-Button, Remove this Delegate from the Button and replace it with my own method which calls the OnRemove on the Collection, call the saved Delegate and after that call the OnRemoveComplete of the Collection.

And this is the point where I stuck.
I can get the Eventinfo from the Button, I can make a MethodInfo ... but I can't get a Delegate from it.

What I have tried:

VB
Public Class RMCollectionEditor
    Inherits CollectionEditor

    Public Sub New(ByVal type As System.Type)
        MyBase.New(type)
    End Sub

    Protected Overrides Function CreateCollectionForm() As CollectionEditor.CollectionForm
        Dim cForm As CollectionForm = MyBase.CreateCollectionForm()

        Dim myButton() As Control

         ' Remove-Button
        myButton = cForm.Controls.Find("removeButton", True)
        If myButton.Length > 0 Then
  
            Dim myEventInfo As System.Reflection.EventInfo = GetType(Button).GetEvent("Click", Reflection.BindingFlags.NonPublic Or BindingFlags.Public Or Reflection.BindingFlags.Instance)

            Dim myInfo As System.Reflection.MethodInfo = myEventInfo.GetAddMethod(True)
            RepeatDelegate = [Delegate].CreateDelegate(GetType(delegate_OnClick), myInfo, False)
            RemoveHandler myButton(0).Click, [Delegate].CreateDelegate(GetType(delegate_OnClick), myInfo, False)
            AddHandler myButton(0).Click, AddressOf RemoveButton_Handler

        End If

         Return cForm
    End Function


     Private Sub RemoveButton_Handler(sender As Object, e As EventArgs)
        MessageBox.Show("Remove")
     End Sub

End Class



It would be nice if someone could help me to get the next step ...
Posted
Comments
Kornfeld Eliyahu Peter 12-Jun-17 8:34am    
It is maybe about how your collection is? Can you show the code for that?
Ralf Meier 12-Jun-17 9:33am    
I could ... but I Think that it's not really necessary.
It's a Collection which derives from CollectionBase. The Point is, when the On-Methods are called or not called ...
Kornfeld Eliyahu Peter 12-Jun-17 8:38am    
It also may work to override the CollectionEditor's DestroyInstance method...
Ralf Meier 12-Jun-17 9:36am    
No ... the point is the Behaviour during Designtime. Here I remove normally only one or two inserted classes ...
I must know (for the following part of the Programm) which class-object is removed - not that one (or more) class-object is (are) removed.
[no name] 12-Jun-17 12:16pm    
Maybe I don't get the Point, but I would agree with Peter to override DestroyInstance and Register the removed objects.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900