How to identify event sender......

  • Thread starter Thread starter Jakub Otahal
  • Start date Start date
J

Jakub Otahal

Hi all,
I have problem........ I have array of buttons and I want to call one
common event service. But I need to know the index of clicked button -- how
can I do that?

thanks
jakub
 
Ok, I knew there was an easier (less code) way to do this...

Basically create your handler function like so:

private void ButtonHandler(object sender, System.EventArgs e) {

MessageBox.Show(sender.ToString());

}





Then, For each button you can select the Events in the Properties box and
change the Click event to point to your ButtonHandler function.


---
John Wiese
---
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Hi,

You can use the sender parameter of the handler to see what control sent
the event, if you have your controls in an array it would be something like:

protected void handler( object o, EventArgs e ){

int index = 0;
foreach( Control control in controlArray)
{
if ( sender == control )
break;
index++;
}
}



hope this help,
 
Jakub Otahal said:
Hi all,
I have problem........ I have array of buttons and I want to call one
common event service. But I need to know the index of clicked button -- how
can I do that?

The button itself is being passed as "sender," but if you really need it's
index you can save it in buttons "Tag" property.
 
Back
Top