C# Handles

Posted on 2009-07-08
Last Modified: 2013-12-17
In vb it is possible to use handles to extend the use of a function/event.

I tried doing the same thing in c# and it did not work.

Here is my code.

protected void UpdateTextBoxes_Textchanged(object sender, EventArgs e)Handle Textbox1.TextChanged, TextBox2.TextChanged


But it does not work... Can anybody help?
Question by:Mr_Shaw
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 100 total points
ID: 24804682
1. Create your event handler (which it looks like you've already done).
2. In the form designer, select one of the TextBox controls
3. Bring up the properties for Textbox1.
4. Click on the lightning bolt icon in the Property window to show the list of events.
5. In the TextChanged event, click the drop down arrow.
6. Select the existing event handler
7. Repeat steps 2 - 6 for the other TextBox controls

Accepted Solution

lppperes earned 150 total points
ID: 24804687

You have to add the handles of the TextChanged event in Form_Load, like this:
        private void Form1_Load(object sender, EventArgs e)
            textBox1.TextChanged += new EventHandler(UpdateTextBoxes_Textchanged);
            textBox2.TextChanged += new EventHandler(UpdateTextBoxes_Textchanged);

        private void UpdateTextBoxes_Textchanged(object sender, EventArgs e)
            textBox3.Text = ((TextBox)sender).Text;

Assisted Solution

Pablissimo earned 150 total points
ID: 24804701
In C# the syntax is slightly different - the += operator is overloaded to 'add' an event handler (a delegate) to an event.

In your case, somewhere in your form's constructor after InitialiseComponent is called you could use code similar to as follows:

TextBox1.TextChanged += UpdateTextBoxes_Textchanged;
TextBox2.TextChanged += UpdateTextBoxes_Textchanged;

You'll still need your Textchanged method to be implemented somewhere in the class:

protected void UpdateTextBoxes_Textchanged(object sender, EventArgs e)
// Handle the text changed here

Notice that we're not calling UpdateTextBoxes_Textchanged (no parentheses) but simply specifying that the TextChanged event of TextBox1 (and 2) should call UpdateTextBoxes_Textchanged.

Take a look at
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


Author Comment

ID: 24804944
Here is my code... It does not do anything.
I have put breakpoints in and it never gets to the event.

    protected void btnNext_Click(object sender, EventArgs e)
    protected void updateDB()
        TextBox Comments = (TextBox)FormView1.FindControl("Textbox_Comments");
        Comments.TextChanged += UpdateTextBoxes_Textchanged;
    protected void UpdateTextBoxes_Textchanged(object sender, EventArgs e)
LVL 15

Assisted Solution

jinal earned 100 total points
ID: 24805238
1. TextChanged event is not fired for each character you enter but it fires after you lost fcous from that control.

2. Try to use ItemCreated Event of formview and use Comments.TextChanged += UpdateTextBoxes_Textchanged;

Author Comment

ID: 24805314

Author Comment

ID: 24805427
I used the event ItemCreated but It returned

Object reference not set to an instance of an object.

Author Closing Comment

ID: 31601630

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question