Sharing control event handlers

V

viboater

This may be a dumb question, so bare with me.

Is it possible to detect which control triggered an event if I share an
Event Handler amoung multiple controls?

For example:


this.THIS_Btn.Click += new System.EventHandler(this.MyBtn_Click);
this.THAT_Btn.Click += new System.EventHandler(this.MyBtn_Click);


private void CancelBtn_Click(object sender, System.EventArgs e)
{
if THIS_Btn_Clicked
do_this();
else if THAT_Btn_Clicked
do_that();
}


Thanks
 
T

Tom Porterfield

This may be a dumb question, so bare with me.

Is it possible to detect which control triggered an event if I share an
Event Handler amoung multiple controls?

For example:

this.THIS_Btn.Click += new System.EventHandler(this.MyBtn_Click);
this.THAT_Btn.Click += new System.EventHandler(this.MyBtn_Click);

private void CancelBtn_Click(object sender, System.EventArgs e)
{
if THIS_Btn_Clicked
do_this();
else if THAT_Btn_Clicked
do_that();
}

Thanks

The sender parameter is the actual control that raised the event.
 
T

Tom Clement

In other words, you can use the following code:

private void MyBtn_Click(object sender, System.EventArgs e)
{
if( sender == THIS_Btn )
do_this();
else if( sender == THAT_Btn )
do_that();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top