C# .NET determine which control triggered event handler

wlevy
wlevy used Ask the Experts™
on
In a C# .NET program I am trying to creat a generic event handler.  How do I determine which form control triggered this event handler at runtime?  I examined the sender parameter but I don't see the control's name anywhere.  In this particular case I am responding to one of several checkboxes with no associated text.

        private void CheckedChanged(object sender, EventArgs e)
        {
            // What property of sender can I use here?
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sean McIlvennaSr. Software Architect

Commented:
private void CheckedChanged(object sender, EventArgs e)
{
  if ( sender == this._checkbox1 ) // do stuff
  else if ( sender == this._checkbox2 ) // do different stuff
  else // perhaps throw an exception because of unknown control
}
private void CheckedChanged(object sender, EventArgs e)
{
    CheckBox TempBox = (CheckBox)sender;
}

TempBox will be set to the CheckBox that triggered the event and will have all of the same properties and attributes (including ID)
Carl TawnSenior Systems and Integration Developer
Commented:
If its always going to be a checkbox then:

        private void CheckedChanged(object sender, EventArgs e)
        {
                CheckBox c = (CheckBox)sender;

                 if (c.Name == "Something")
                 {
                 }
                 else if (c.Name == "Something else")
                 {    
                 }
                  else
                  {
                            // potential default handler
                   }
        }

Author

Commented:
Thanks very much, this is exactly what I needed!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial