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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:35+00:00 2026-05-30T13:56:35+00:00

I am learning about event handlers and delegates. I have a single form with

  • 0

I am learning about event handlers and delegates. I have a single form with 4 text boxes and a list box. I’d like to have a delegate that listens for text box changes to ANY of the 4 boxes. The method associated with the delegate would simply be a method that takes the text value of the text box that changed, and add that as a new list item. My question is how to write the delegate to listen to all of the text boxes, and when I call the function to add the list box item, how can I pass in the text box object since I won’t know explicitly which one raised the event? Will this be contained in the EventArgs e?

Rather than use multiple event handlers:

this.textBo1.TextChanged += txt_TextChanged;
this.textBo2.TextChanged += txt_TextChanged;
this.textBo3.TextChanged += txt_TextChanged;
this.textBo4.TextChanged += txt_TextChanged;

I’d like something like:

public delegate ListenToTextBoxes(object sender, EventArgs e);

Maybe that doesn’t make sense since I’m new to delegates, but it seems reasonable to me that I should be able to make one delegate listen to text box controls in general, and then when it raises the event I cast the object sender and get the text box text. But how do I create the delegate so it listens ONLY to text boxes, or other types of controls?

  • 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-05-30T13:56:36+00:00Added an answer on May 30, 2026 at 1:56 pm

    What about something like this ?

    // form constructor
    public MyForm()
    {
       InitializeComponent();
       this.textBo1.TextChanged += txt_TextChanged;
       this.textBo2.TextChanged += txt_TextChanged;
       this.textBo3.TextChanged += txt_TextChanged;
       this.textBo4.TextChanged += txt_TextChanged;
    }
    
    // event handler
    void txt_TextChanged(object sender, EventArgs e)
    {
       var textBox = (TextBox)sender;
       this.myList.Add(textBox.Text);
    }
    

    EDIT :
    (according to the question update)

    Actually, the previous code can easily modified to created one single delegate and pass it to all the textboxes, but it doesn’t change too much (there’re still four event subscriptions):

    public MyForm()
    {
        InitializeComponent();
    
        // EventHandler is defined as:
        // delegate void EventHandler(object sender, EventArgs e)
        // so its signature is equal to your delegate:
        // delegate void ListenToTextBoxes(object sender, EventArgs e);
        // Hence, if you receive that delegate from somewhere out, you can pass it
        // to TextChange events
        var myDelegate = new EventHandler(txt_TextChanged);
    
        this.textBox1.TextChanged += myDelegate;
        this.textBox2.TextChanged += myDelegate;
        this.textBox3.TextChanged += myDelegate;
        this.textBox4.TextChanged += myDelegate;
    }
    

    The problem is that you do need to subscribe the TextChanged event for each TextBox control, there’s no magic way to say “register all the TextBox.TextChange events to this delegate”.
    But, if you are sure that all the textboxes that you want to register are children of this control, you can use a loop, e.g. :

    public MyForm()
    {
        InitializeComponent();
        var myDelegate = new EventHandler(txt_TextChanged);
        foreach (var ctrl in this.Controls)
        {
            var txtBox = ctrl as TextBox;
            if (txtBox != null)
                txtBox.TextChanged += myDelegate;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning delegates in VB.NET and am confused about delegate types. In reading about
After learning about the Action delegate in C# I've been looking for ways I
I have been learning about the basics of C# but haven't come across a
I am just learning about app.config in respect of creating custom sections. I have
I'm learning event-driven programming by Java now, but most materials are about swing. I
I'm learning events in C# and understand that the EventArgs class carries data about
Whilst learning events and delegates I can't help but think about the Observer design
I am learning about events in C#. I found out that they are just
In my class we have been learning about different designs such as MVC and
I am learning about preconditions and when to use them. I have been told

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.