473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
2 22967
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mohit Gupta | last post by:
Hi all, Lately I have been working on an application in VB .net CF for Pocket PC device. I have a small question about Context Menu. When I try to close the window after context menu is poped...
5
by: Dean L. Howen | last post by:
Dear friends, Could we determine when context menu should appear?
2
by: Dino M. Buljubasic | last post by:
I have several context menus added to my form. The form is displaying items in listviews connected to the context menus. When I click on an item in a list view, a popup context menu shows allowing...
1
by: Rory | last post by:
Hi - I've seen quite a few posts that almost match this but haven't found a solution that works. I have an explorer-like application with a treeview in the lefthand pane. When I right-click on a...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
3
by: =?Utf-8?B?ZWFndWlsYXI=?= | last post by:
Hi, I am trying to dynamically generate a menu, based on entries on a text or xml file. The text file contains the "tree" after which the menu will need to be created. Something like the...
1
by: Griff | last post by:
Hi I have a context menu with several menu items. When the user selects the menu item they want, then I obviously want to identify which menu item was selected. I've managed to handle the...
0
by: EvilProject | last post by:
Hi im writing a class of a context menu where each menu item is linked to a Tree Node. I have a click event (that belongs to the context menu class ,not the menu item class) that it's event...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.