Skip to content

c# and vb.net sample to remove eventhandler with reflection

This is a sample how to remove a (non-public) eventhandler in c# and vb.net.

//c# sample
using System;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Form2 form2 = new Form2();
            //remove eventhandler with reflection
            EventInfo ei = form2.GetType().GetEvent("Load");
            Delegate d = Delegate.CreateDelegate(typeof(EventHandler), form2, "Form2_Load", false);
            ei.RemoveEventHandler(form2, d);

            //messagebox is not shown, since the eventhandler for load was removed
            form2.ShowDialog();
        }
    }
}

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form2_Load);
        }

        void Form2_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
    }
}
'vb.net sample
Imports System.Reflection

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim myForm As New Form2
        Dim type As Type = GetType(Form2)

        'remove eventhandler with reflection
        Dim eventInfo As EventInfo
        eventInfo = type.GetEvent("Load")
        Dim loadHandler As System.Delegate
        loadHandler = System.Delegate.CreateDelegate(GetType(EventHandler), myForm, "Form2_Load", False)
        eventInfo.RemoveEventHandler(myForm, loadHandler)

        'messagebox is not shown, since the eventhandler for load was removed
        myForm.ShowDialog()

    End Sub
End Class

Public Class Form2
    Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        MessageBox.Show("hello world")
    End Sub
End Class


RemoveEventHanlderWithReflection.zip

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

sunsu on :

How can you remove all handlers from a control, ont only "Form2_Load" ?

assignment help uk on :

C# and VB.NET are top notch programming dialects that depend on the Microsoft .NET Framework, and they are similarly intense. Both Visual Basic .NET and Visual C# .NET utilize the normal dialect runtime.

mobile home dealers austin tx on :

Your web has a considerable measure of valuable data for myself. I visit routinely. Want to have greater quality things.

https://resume-writing-services.reviews/zipjob-com-review/ on :

numerous schools and colleges give composing workshops, in any case, to help with continue composing. In the event that you are in school, go to your vocation advancement office or your instructive learning focus and request more data about resume composing workshops.

psychology research paper topics on :

Reflection is the throwing back or deflection of waves, before-mentioned as light or sound waves if they hit a surface. Reflection happens whenever light falls on an object. The law of reflection states that the point of incidence (the angle between the ray and a perpendicular line drawn to the outside) is similar to the angle of reflection (the angle between the considered ray and a perpendicular to the outside).

Economics Essay Writer on :

I am facing the same coding errors on my website too and I don't have knowledge about the coding so I also need a solution to it. It will be a pleasure to get the response here from you guys.

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
Form options