469,283 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Casting a 'sender' argument in an event handler

Jay
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;
}
Dec 20 '06 #1
6 7632
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;
}
Dec 20 '06 #2
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
Dec 20 '06 #3
Jay
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" <nospamwrote in message news:OC****************@TK2MSFTNGP02.phx.gbl...
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;
}
Dec 20 '06 #4
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
Dec 20 '06 #5
Jay
Thanks again for your help.

Jay

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
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

Dec 20 '06 #6
Dale <da******@nospam.nospamwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Christoph Boget | last post: by
2 posts views Thread by Thorsten moeller | last post: by
4 posts views Thread by poppy | last post: by
6 posts views Thread by rn5a | last post: by
7 posts views Thread by Christopher Pisz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.