Casting a 'sender' argument in an event handler

  • Thread starter Thread starter Jay
  • Start date Start date
J

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;
}
 
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
 
You need to cast the object first.

TextBox myTextBox = (TextBox)sender;

/Niklas

Jay skrev:
 
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 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;
}
 
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
 
Thanks again for your help.

Jay

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
 
Dale said:
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.
 
Back
Top