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 8570139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:29:30+00:00 2026-06-11T18:29:30+00:00

I would pass a control of my form to another class where I will

  • 0

I would pass a control of my form to another class where I will be creating events for the control, its parent control etc. I also need to detach those events at some point. But I need to ensure I wont be duplicating the events, if the event is already attached.

So I always attach events like this, for eg:

internal static void X(Control c, MouseEventHandler mouseDownEvent)
{
    c.TopLevelControl.MouseDown -= mouseDownEvent;
    c.TopLevelControl.MouseDown += mouseDownEvent;
}

Now I would need similar event attaching for other handlers too. For eg,

internal static void X(Control c, EventHandler event)
{
    c.Enter -= event;
    c.Enter += event;
}

Now I dont want to spray around this -= and += all around, instead would like to have one simple utility function so that I can call it everywhere.

Something like:

internal static void AttachEvent(this Control c, 
                                 Func<Control, MouseEventHandler> e, 
                                 MouseEventHandler m)
{
    e(c) -= m;
    e(c) += m;
}

So that I can call:

AttachEvent(c, control => control.MouseDown, mouseDownEvent);

But this wouldn’t compile, I get two errors:

The event 'System.Windows.Forms.Control.MouseDown' can only appear on the left hand side of += or -=

and

The left-hand side of an assignment must be a variable, property or indexer

I would like to have the AttachEvent take any event as input argument, but if that’s too complicated, I can live with the MouseEvents alone.

  • 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-11T18:29:31+00:00Added an answer on June 11, 2026 at 6:29 pm

    I think this is not possible. Event in C# – like a property – consists of two methods: add and remove. So if you expand event in two methods in your mind

    e.add_MouseOver(Delegate)
    e.remove_MouseOver(Delegate) 
    

    you will see it is not possible to pass reference to event and do something with it in other method.

    Also += is part of C# syntax and only works with referencing event at left side of expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to pass data from a database column to another page via
I would like to pass values into the constructor on the class that implements
I have a form that has label values that I would like to pass
I would like to pass the reference of a variable into a class, use
Having a treeview control on a form which will be used to visually represent
I would like to create a new dynamic chart control. This chart will have
i have 2 classes(a form class and a user-control class) and in the form
My brain is fried, so I thought I would pass this one to the
I would like to pass a MYSQL query via Coldfusion the following date: 03/13/2010
I would like to pass the multiple arguments with positive or negative values. Is

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.