473,386 Members | 1,699 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,386 software developers and data experts.

a way to make just one event handler to handle multiple events from diff. objects ?

Shenno
59
hi, how is all ??

i have just got a problem, i have a form with about 30 text boxes.

i have made these event hundlers to handle got focus and lose focus events
Expand|Select|Wrap|Line Numbers
  1.         private void gf(object sender, EventArgs e)
  2.         {
  3.             name.BackColor=System.Drawing.SystemColors.Info;
  4.         }
  5.         private void lf(object sender, EventArgs e)
  6.         {
  7.             name.BackColor = System.Drawing.SystemColors.Window;
  8.         }
and applied it for "name" textbox


is there a way to apply the same event hundler to all form textboxes without making event handler for every one of my 30 textboxes ??

thx in advance.
Sep 9 '10 #1

✓ answered by MartijnHoekstra

What I believe you want to do can be done like this:

Expand|Select|Wrap|Line Numbers
  1. private void textBox_GetFocus( object sender,  EventArgs e)
  2. {
  3.    TextBox tb = sender as TextBox;
  4.    if (tb) tb.BackColor=System.Drawing.SystemColors.Info;
  5. }
  6.  
  7. private void textBox_LoseFocus( object sender,  EventArgs e)
  8. {
  9.    TextBox tb = sender as TextBox;
  10.    if(tb) tb.BackColor = System.Drawing.SystemColors.Window;
  11. }
if sender is unable to be casted to a TextBox, tb will be 'null', hence the test.

now you need to set the same event handler to each textbox you wish.

5 1627
Yes that is possible..
Let all textboxes refer to the same eventhandler.

Within the handler, the 'sender' will refer to the Textbox sending the event.
Casting the sender being of type 'object' back to type 'Textbox', will allow you to query its name so you can see which one it was.
You could also make use of the 'Tag' member, place some number in there for your reference
Sep 9 '10 #2
Shenno
59
thx ,but i didn't understand very much, can u give me code sample ?
Sep 9 '10 #3
What I believe you want to do can be done like this:

Expand|Select|Wrap|Line Numbers
  1. private void textBox_GetFocus( object sender,  EventArgs e)
  2. {
  3.    TextBox tb = sender as TextBox;
  4.    if (tb) tb.BackColor=System.Drawing.SystemColors.Info;
  5. }
  6.  
  7. private void textBox_LoseFocus( object sender,  EventArgs e)
  8. {
  9.    TextBox tb = sender as TextBox;
  10.    if(tb) tb.BackColor = System.Drawing.SystemColors.Window;
  11. }
if sender is unable to be casted to a TextBox, tb will be 'null', hence the test.

now you need to set the same event handler to each textbox you wish.
Sep 9 '10 #4
Shenno
59
Error ^_^

needs "As" to be ";"
Sep 9 '10 #5
Shenno
59
thx, it's solved like this

Expand|Select|Wrap|Line Numbers
  1.         private void lf(object sender, EventArgs e)
  2.         {
  3.             TextBox tb = (TextBox)sender; 
  4.              tb.BackColor=System.Drawing.SystemColors.Window; 
  5.         }
  6.  
  7.  

u still the one <3
Sep 9 '10 #6

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

Similar topics

1
by: steph | last post by:
In VC++ . NET designer, if I double click on a button on my form , the IDE generates an event handler for Click event. Is there a way to generate event handler for other events like MouseOver,...
2
by: David A. Beck | last post by:
I would to set up a event routine for several command buttons. I need to be able to identify by the caption or something whick button caused the event to trigger. I think it's in the "sender" or...
2
by: schiefaw | last post by:
Hello, I created a custom event "Public Event ApprovalChangedEvent()" and set a function to catch it "Private Sub laset_ApprovalChangedEvent() Handles laset.ApprovalChangedEvent". The problem...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
3
by: jimcolli | last post by:
I have a parent form with a menu button that has a handler. I want to call this same handler when a button on a child form is clicked. I have this simplified code in the main form's Load...
3
by: akki | last post by:
I create link buttons dynamically in the code and give them IDs through which I can distinguish them.I want to know how to create a single event handler for all the link buttons that I create The...
1
by: Christian Sengstock | last post by:
Hi all, i have a js application with several event handlers which i manage via prototype .bindAsEventListener. On normal page navigation ( link navigation ) i finish all events handlers...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
7
by: Andrus | last post by:
I noticed that DataGridView CellValidated() and other event handler first parameter is object: Grid.CellValidated+=new DataGridViewCellEventHandler(Grid_CellValidated); .... void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.