By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Mar 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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[i].Tag = contextMenuStrip1;
}
}

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
ContextMenuStrip test = (ContextMenuStrip)((ToolStripMenuItem)sender).Tag;
object caller = test.SourceControl;
}
Mar 16 '07 #2

P: n/a
Bry
On 16 Mar, 11:32, Vincent Finn <1...@2.comwrote:
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[i].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.

Mar 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.