Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Visual-Studio VB.NET
Hi every one
i have two forms
Form1 and Form2
in form2 i have a event
Public Class Form2
 
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MsgBox("This is form2")
    End Sub
End Class
 
now i want to removehandler Form2_Load before call it
 
i found this code for get load delegate
  Dim FRM2 As Form = New Form2
        Dim assem As [Assembly] = [Assembly].GetExecutingAssembly()
        Dim tExForm As Type = assem.GetType("WindowsApplication2.Form2")
        Dim exFormAsObj As Object = _
            Activator.CreateInstance(tExForm)
        Dim evClick As EventInfo = tExForm.GetEvent("Load")
        Dim tDelegate As Type = evClick.EventHandlerType
 
        Dim handler As New DynamicMethod( _
           "", _
           Nothing, _
           GetDelegateParameterTypes(tDelegate), _
           GetType(Form1) _
       )
        Dim ilgen As ILGenerator = handler.GetILGenerator()
 
        Dim showParameters As Type() = {GetType(String)}
        Dim simpleShow As MethodInfo = _
            GetType(MessageBox).GetMethod("Show", showParameters)
 
        ilgen.Emit(OpCodes.Ldstr, _
            "This event handler was constructed at run time.")
        ilgen.Emit(OpCodes.Call, simpleShow)
        ilgen.Emit(OpCodes.Pop)
        ilgen.Emit(OpCodes.Ret)
        Dim dEmitted As [Delegate] = handler.CreateDelegate(tDelegate)
 
but when removehandler not works on that
 RemoveHandler FRM2.Load, dEmitted
 
        FRM2.ShowDialog()
 
i try this code but it's not work too
evClick.RemoveEventHandler(FRM2, dEmitted)
FRM2.ShowDialog()
 
Where is my problem?
thank you.
Posted 12-Mar-13 22:01pm
Edited 12-Mar-13 22:03pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Delegate or not, it does not really matter.
 
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].
 
Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].
 
—SA
  Permalink  
Comments
Espen Harlinn at 13-Mar-13 12:07pm
   
5'ed!
Sergey Alexandrovich Kryukov at 13-Mar-13 13:35pm
   
Thank you, Espen.
—SA

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 13 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100