473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C# Custom Event Handler replicating textBox1_TextChanged event handler

Howdy!

I want to write a Custom Event Handler replicating textBox1_TextChanged event handler to update textBox2 with textBox1 text as I type. I tried the following in vain. Please help.



The code snippet:

// CustomEventArgs.cs

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     delegate void CustomEventHandler(object sender, CustomEventArgs e);
  9.  
  10.     public class CustomEventArgs : EventArgs
  11.     {
  12.         string TextBoxText;
  13.  
  14.         public CustomEventArgs(string TextBoxText)
  15.         {
  16.             this.TextBoxText = TextBoxText;
  17.         }
  18.  
  19.         internal string TextBoxTextChanged
  20.         {
  21.             get
  22.             {
  23.                 return TextBoxText;
  24.             }
  25.         }
  26.     }
  27. }
  28.  
// Form1.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();          
  17.         }
  18.  
  19.         private event CustomEventHandler TextBox1Event;
  20.  
  21.         private void TextBox1_CustomTextChanged(object sender, CustomEventArgs e)
  22.         {
  23.             textBox2.Text = e.TextBoxTextChanged;
  24.         }
  25.  
  26.         private void OnTextBox1Event(CustomEventArgs e)
  27.         {
  28.             if (TextBox1Event != null)
  29.             {
  30.                 TextBox1Event(this, e);
  31.             }
  32.         }
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.             TextBox1Event += new CustomEventHandler(TextBox1_CustomTextChanged);
  37.             CustomEventArgs TextBox1EventArgs = new CustomEventArgs(textBox1.Text);
  38.             OnTextBox1Event(TextBox1EventArgs);
  39.         }
  40.    }
  41. }
  42.  
Thanks.
Nov 17 '09 #1
1 3461
Plater
7,872 Expert 4TB
why not just update one from the other's textchanged event?
Nov 17 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mg | last post by:
In a Web Form, I have a TextBox: protected System.Web.UI.WebControls.TextBox TextBox1; At design time, I double click on the TextBox and get an event handler: private void...
4
by: Mark Lingen | last post by:
I've found a problem with postback event handling and webcontrol buttons. Try out the following code in an ASP.Net project and you will see. Create a web project in VB.Net and drop this code...
4
by: Alexander Mueller | last post by:
Hi all, in a CheckedListBox there are afaics two members that change the checked-flag for an item i.e. SetItemChecked and SetItemCheckState. In the online-help there is a remark that...
3
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the...
2
by: Ivan Sammut | last post by:
Hi, I have a form which contains about 100 Textboxes but I mapped all their onTextchanged event to the same procedure. Now I would like to know if it is possible from the sender argument passed...
3
by: Tony | last post by:
I have a WebApp with 3 text boxes and 1 button, as described below. Can someone please explain why the bad scenarios are occurring and better still if they can be prevented or worked-around?...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.