Context Menu Click event - how to Identify the object that was right clicked

  • Thread starter Thread starter Bry
  • Start date Start date
B

Bry

I have a context menu which is shared between a TreeView and ListView
control (both controls show the same information, similar to how
Windows Explorer works, so it makes sense to use the same menu on both
views).

What i'm struggling to do when the menuItem_Click event is fired, is
to detect the TreeNode or ListViewItem that was originally right
clicked when the context menu was opened.

I can't just look at the selected item, because I don't even know in
which control the context menu was initiated. Can anyone offer any
clues?

I could easily store a copy of the TreeNode or ListViewItem in the
menu tag, but the _Click event doesn't seem to provide a means of
accessing the menu via the sender variable.

Regards,
Bry
 
I have a context menu which is shared between a TreeView and ListView
control (both controls show the same information, similar to how
Windows Explorer works, so it makes sense to use the same menu on both
views).

What i'm struggling to do when the menuItem_Click event is fired, is
to detect the TreeNode or ListViewItem that was originally right
clicked when the context menu was opened.

I can't just look at the selected item, because I don't even know in
which control the context menu was initiated. Can anyone offer any
clues?

I could easily store a copy of the TreeNode or ListViewItem in the
menu tag, but the _Click event doesn't seem to provide a means of
accessing the menu via the sender variable.

Regards,
Bry

Hi,

It's a bit of a hack but you can set the tag of the item to be the menustrip.
Then you can get that back from the sender in your event and use the SourceControl
property.

Functional but ugly.

Vin

// Code /////////////////
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;

public Form1()
{
InitializeComponent();
for (int i = 0; i < contextMenuStrip1.Items.Count; ++i)
{
contextMenuStrip1.Items.Tag = contextMenuStrip1;
}
}

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
ContextMenuStrip test = (ContextMenuStrip)((ToolStripMenuItem)sender).Tag;
object caller = test.SourceControl;
}
 
I have a context menu which is shared between a TreeView and ListView
control (both controls show the same information, similar to how
Windows Explorer works, so it makes sense to use the same menu on both
views).
What i'm struggling to do when the menuItem_Click event is fired, is
to detect the TreeNode or ListViewItem that was originally right
clicked when the context menu was opened.
I can't just look at the selected item, because I don't even know in
which control the context menu was initiated. Can anyone offer any
clues?
I could easily store a copy of the TreeNode or ListViewItem in the
menu tag, but the _Click event doesn't seem to provide a means of
accessing the menu via the sender variable.
Regards,
Bry

Hi,

It's a bit of a hack but you can set the tag of the item to be the menustrip.
Then you can get that back from the sender in your event and use the SourceControl
property.

Functional but ugly.

Vin

// Code /////////////////
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;

public Form1()
{
InitializeComponent();
for (int i = 0; i < contextMenuStrip1.Items.Count; ++i)
{
contextMenuStrip1.Items.Tag = contextMenuStrip1;
}

}

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
ContextMenuStrip test = (ContextMenuStrip)((ToolStripMenuItem)sender).Tag;
object caller = test.SourceControl;

}


Vincent,

Thanks for the reply.

I had wondered doing it that way, but then I realised that I don't
have to actually access the context menu object via the sender
property.

As the context menu is a private member of the class (created when the
form is created), I can simply access the Tag property of the context
menu control directly in the _Click event.

Thanks,
Bry.
 
Back
Top