PC Review


Reply
Thread Tools Rate Thread

Casting a 'sender' argument in an event handler

 
 
Jay
Guest
Posts: n/a
 
      20th Dec 2006
I'm using VS 2005 C# Express.

I have an event handler that is called when the text cursor leaves one of many different text boxes.
When this is called, I want the text colour in that text box to change to Red. I assume that the
"sender" argument in the call is the text box that triggered the event. When I tried to change the
text colour using sender.ForeColor = System.Drawing.Color.Red; I get a compile error: 'object' does
not contain a definition for 'BackColor'. This didn't surprise me since I'm a C# beginner. Is there
some sort of cast I can use to make it work, or do I have to use if... else if... etc statements to
check for each of the text boxes that might have triggered the event (eg one is called tbxFbeg)?
Here's a fragment of my code...

private void tbxSettingsSa_Leave(object sender, EventArgs e){
sender.ForeColor = System.Drawing.Color.Red; //get compile error: 'object' does not contain a
definition for 'BackColor'
// sender is a textbox. The following line works OK...
// tbxFbeg.ForeColor = System.Drawing.Color.Red;
}


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      20th Dec 2006
Yep; a straight cast... for bullet-proofing I'll illustrate the case
where you *hope* is is a textbox, but aren't sure...

TextBox tb = sender as TextBox;
if(tb!=null) { // sender was (as expected) a TextBox
tb.ForeColor =System.Color.Red;
}

Of course, if your handler only ever deals with a single control, you
can code directly against the field (as per your "this works"
example).

Marc


 
Reply With Quote
 
niklas.arbin@nordicstation.com
Guest
Posts: n/a
 
      20th Dec 2006
You need to cast the object first.

TextBox myTextBox = (TextBox)sender;

/Niklas

Jay skrev:

> I'm using VS 2005 C# Express.
>
> I have an event handler that is called when the text cursor leaves one of many different text boxes.
> When this is called, I want the text colour in that text box to change to Red. I assume that the
> "sender" argument in the call is the text box that triggered the event. When I tried to change the
> text colour using sender.ForeColor = System.Drawing.Color.Red; I get a compile error: 'object' does
> not contain a definition for 'BackColor'. This didn't surprise me since I'm a C# beginner. Is there
> some sort of cast I can use to make it work, or do I have to use if... else if... etc statements to
> check for each of the text boxes that might have triggered the event (eg one is called tbxFbeg)?
> Here's a fragment of my code...
>
> private void tbxSettingsSa_Leave(object sender, EventArgs e){
> sender.ForeColor = System.Drawing.Color.Red; //get compile error: 'object' does not contain a
> definition for 'BackColor'
> // sender is a textbox. The following line works OK...
> // tbxFbeg.ForeColor = System.Drawing.Color.Red;
> }


 
Reply With Quote
 
Jay
Guest
Posts: n/a
 
      20th Dec 2006
Thanks Marc + Niklas for your replies. It now works.

I've also found that this works, which is a little more compact. I assume it's just as good, but do
say if it isn't (I know for sure that it is always a textbox).

((TextBox)sender).ForeColor = System.Drawing.Color.Red;

Jay


"Jay" <nospam> wrote in message news:OChsp%(E-Mail Removed)...
I'm using VS 2005 C# Express.

I have an event handler that is called when the text cursor leaves one of many different text boxes.
When this is called, I want the text colour in that text box to change to Red. I assume that the
"sender" argument in the call is the text box that triggered the event. When I tried to change the
text colour using sender.ForeColor = System.Drawing.Color.Red; I get a compile error: 'object' does
not contain a definition for 'BackColor'. This didn't surprise me since I'm a C# beginner. Is there
some sort of cast I can use to make it work, or do I have to use if... else if... etc statements to
check for each of the text boxes that might have triggered the event (eg one is called tbxFbeg)?
Here's a fragment of my code...

private void tbxSettingsSa_Leave(object sender, EventArgs e){
sender.ForeColor = System.Drawing.Color.Red; //get compile error: 'object' does not contain a
definition for 'BackColor'
// sender is a textbox. The following line works OK...
// tbxFbeg.ForeColor = System.Drawing.Color.Red;
}


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      20th Dec 2006
Then yes, that is absolutely fine. The variable approach is useful
when you want to apply more than one operation to the same object -
i.e. set a few properties rather than just one. Otherwise you need to
cast each time, or create a wrapper method (and cast the parameter to
the method).

Marc


 
Reply With Quote
 
Jay
Guest
Posts: n/a
 
      20th Dec 2006
Thanks again for your help.

Jay

"Marc Gravell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
Then yes, that is absolutely fine. The variable approach is useful
when you want to apply more than one operation to the same object -
i.e. set a few properties rather than just one. Otherwise you need to
cast each time, or create a wrapper method (and cast the parameter to
the method).

Marc



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Dec 2006
Dale <(E-Mail Removed)> wrote:
> I agree with Marc's initial recommendation. You should do the type checking
> that the "as" statement does and then check for null.


That depends on whether it not being a TextBox will always mean there's
a coding error. If it does, the exception generated from the failed
cast is entirely appropriate - using "as" would mask the error.

I only use "as" when the cast could legitimately fail in a non-error
case.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sender object in event handler Ant Microsoft ADO .NET 4 26th Mar 2008 12:31 AM
Event Handler that creates adds another event handler kaczmar2@gmail.com Microsoft ASP .NET 1 22nd Feb 2007 07:37 AM
Reusable event handler - identify sender Me Microsoft C# .NET 4 11th Nov 2006 04:52 PM
How to Remove an Event Handler without knowing the name of the handler Charles Law Microsoft VB .NET 4 10th Jun 2004 03:48 PM
Create new event handler or override existing handler? =?Utf-8?B?V29vbiBLaWF0?= Microsoft Dot NET Framework Forms 1 5th Jan 2004 08:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.