Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

C# Handles

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
Mr_Shaw
Asked:
Mr_Shaw
4 Solutions
 
Craig WagnerSoftware ArchitectCommented:
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
 
lppperesCommented:
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
 
PablissimoCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mr_ShawAuthor Commented:
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
 
jinalCommented:
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
 
Mr_ShawAuthor Commented:
ItemCreated?
0
 
Mr_ShawAuthor Commented:
I used the event ItemCreated but It returned

Object reference not set to an instance of an object.
0
 
Mr_ShawAuthor Commented:
thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now