One ContextMenu assigned to many controls - which control triggered it?

P

PeterZ

Hi,

I have a single context menu assigned to 10 controls (DataGrids) on a form.

In the context menu item click event I need to determine which control
(DataGrid) is invoking this menu item. Something like this:



private void cmnuItem1_Click(object sender, System.EventArgs e)
{
string sInvokingControlName = ..... which DataGrid invoked the context
menu .....

switch(sInvokingControlName)
{
case "DataGrid1"
.... run some operation on datagrid 1
break;
case "DataGrid2"
.... run some operation on datagrid 2
break;
case "DataGrid3"
.... run some operation on datagrid 3
break;
}
}


Any suggestions how I could achieve this?

Thanks,
PeterZ
 
U

uebercoder

Use the sender:

private void cmnuItem1_Click(object sender, System.EventArgs e)
{
Control ctrl = (Control) sender;
string sInvokingControlName = ctrll.Name
.....

}
 
P

PeterZ

Thanks uebercoder.

As I found out, 'sender' is always going to give me the MenuItem control
itself. But it was enough to get me thinking and expand a little further to
get what I want (the control which triggered the context menu).....


private void cmnuItem1_Click(object sender, System.EventArgs e)
{
// Get the control which triggered the context menu.
MenuItem mi = (MenuItem)sender;
ContextMenu cm = mi.GetContextMenu();
Control control = cm.SourceControl;

if (control is DataGrid)
{
string sInvokingControlName = control.Name
 

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