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

  • Home
  • SEARCH
  • 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 8066421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:59:24+00:00 2026-06-05T11:59:24+00:00

Question: How can I create a function that attaches multiple event handlers to multiple

  • 0

Question:
How can I create a function that attaches multiple event handlers to multiple controls?

Intent:
I am using C# to develop a windows forms application. I want to create a function that takes a collection of controls and a collection of event handlers. This function will attach the event handlers to the controls. What would be the most elegant, and reusable way to do this. I have a pretty ugly way to do this with delegates, but it is a but it is less work for me to just throw this into a loop, and abandoning the function.

Behavior I basically want:

foreach(Control control in controlCollection)
     foreach(EventHandler handler in eventHandlerCollection)
                    control.Event += handler;

Function:

attachHandlers(? controlCollection, ? eventHandlers)

Edit:
I am just going to subscribe all the handlers to the same event on all the controls. I didn’t explicitly say that in my description, so I believe that is the reason for all of confusion.

  • 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-05T11:59:25+00:00Added an answer on June 5, 2026 at 11:59 am

    If the controls in question inherit from the same base class or interface (or they are the same class), you can do something like:

    void AttachClickEventHandlers(List<IClickableControl> controls, List<MyClickHandler> eventHandlers)
    {
        foreach (var control in controls)
            foreach (MyClickHandler handler in eventHandlers)
                control.Click += handler;
    }
    

    This assumes an interface like:

    public interface IClickableControl
    {
        event MyClickHandler Click;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The answer to this SO question says that you can create an api by
I have a question about joptionpane. Using JOptionPane.showMessageDialog(...), we can create a message dialog.
The question is self-explaining, how to create a tearoff menu using GTKAda? I can't
I am trying to create a function that can convert a month number to
In Oracle and MySQL, how can I create a function that takes an indefinite
I want to create function that can be used either with Id or by
I want to create a function that can take different types of iterators which
I am trying to create a function that can subtract between 2 dates but
I am trying to create a simple function that can take a parameter and
Beginners Javascript question here. I am trying to create a function that finds all

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.