Solved

C# Handles

Posted on 2009-07-08
8
488 Views
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?
0
Comment
Question by:Mr_Shaw
8 Comments
 
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
0
 
LVL 2

Accepted Solution

by:
lppperes earned 150 total points
ID: 24804687
Hi,

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;
        }
0
 
LVL 5

Assisted Solution

by:Pablissimo
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 http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
0
 

Author Comment

by:Mr_Shaw
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)
    {
        updateDB();
    }
    protected void updateDB()
    {
        TextBox Comments = (TextBox)FormView1.FindControl("Textbox_Comments");
        Comments.TextChanged += UpdateTextBoxes_Textchanged;
    }
    protected void UpdateTextBoxes_Textchanged(object sender, EventArgs e)
    {
        Response.Write("Empty");
    }
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Assisted Solution

by:jinal
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;
0
 

Author Comment

by:Mr_Shaw
ID: 24805314
ItemCreated?
0
 

Author Comment

by:Mr_Shaw
ID: 24805427
I used the event ItemCreated but It returned

Object reference not set to an instance of an object.
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31601630
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now