Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:34+00:00 2026-06-09T14:16:34+00:00

Say I have two user controls and I want to remove an event handler

  • 0

Say I have two user controls and I want to remove an event handler from one instance of the control.

To illustrate I’ve just made it a button as user control:

public partial class SuperButton : UserControl
{
public SuperButton()
{
    InitializeComponent();
}

private void button1_MouseEnter(object sender, EventArgs e)
{
    button1.BackColor = Color.CadetBlue;
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    button1.BackColor = Color.Gainsboro;
}
}

I’ve added two super buttons to the form and I want to disable the MouseEnter event firing for SuperButton2.

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    superButton2.RemoveEvents<SuperButton>("EventMouseEnter");
}
}

public static class EventExtension
{
public static void RemoveEvents<T>(this Control target, string Event)
{
    FieldInfo f1 = typeof(Control).GetField(Event, BindingFlags.Static | BindingFlags.NonPublic);
    object obj = f1.GetValue(target.CastTo<T>());
    PropertyInfo pi = target.CastTo<T>().GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
    EventHandlerList list = (EventHandlerList)pi.GetValue(target.CastTo<T>(), null);
    list.RemoveHandler(obj, list[obj]);
}

public static T CastTo<T>(this object objectToCast)
{
    return (T)objectToCast;
}
}

The code runs but it doesn’t work – the MouseEnter and Leave events still fire. I’m looking to do something like this:

superButton2.MouseEnter -= xyz.MouseEnter;

Update: Read this comments questions…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:16:36+00:00Added an answer on June 9, 2026 at 2:16 pm

    In your case, you don’t need to remove all event handlers at once, just the specific one you’re interested in. Use -= to remove a handler in the same way you use += to add one:

    button1.MouseEnter -= button1_MouseEnter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control where I have two panels, one is on the
I have created one user control to pick date from calendar. I used jquery
Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor
Let's say I have two entities: Group and User. Every user can be member
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port
Let's say you have two classes that extend UserControl . Each of the controls
Say I have two tables User ----- id first_name last_name User_Prefs ----- user_id pref
I have two questions. First one is simple: Is there any way to control
I'm having an issue where I have made a user control with two content
So for example... in the following user control I have a grid with two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.