Obtaining a reference to the TreeView item in a context menu
-
I've created a TreeView which lists items from active directory. With the help of another Code Project user, I've created a context menu which is specific to the type of object right-clicked-on by the user. The problem is that when I select the menu option, the 'sender' holds a reference to the MenuItem, not the TreeNode clicked-on in the first place. How can I get a reference to the original TreeNode that the user right-clicked on? I've thought of one way but it seems a bit of a botch: to create a TaggedMenuItem class inherited from MenuItem which has a 'Tag' like Controls have and to use the MouseUp event and populate the Tag of EVERY TaggedMenuItem on the context menu with a reference to the TreeNode as I create the context menu. Then, I can get back to it from the menu item click event handler. Like I said, this sounds a bit crap. Is there a better way?
-
I've created a TreeView which lists items from active directory. With the help of another Code Project user, I've created a context menu which is specific to the type of object right-clicked-on by the user. The problem is that when I select the menu option, the 'sender' holds a reference to the MenuItem, not the TreeNode clicked-on in the first place. How can I get a reference to the original TreeNode that the user right-clicked on? I've thought of one way but it seems a bit of a botch: to create a TaggedMenuItem class inherited from MenuItem which has a 'Tag' like Controls have and to use the MouseUp event and populate the Tag of EVERY TaggedMenuItem on the context menu with a reference to the TreeNode as I create the context menu. Then, I can get back to it from the menu item click event handler. Like I said, this sounds a bit crap. Is there a better way?
You can subscribe to the MouseDown event and figure out which node was right-clicked on with the help of the mouse coordinates.
Point point = new Point(e.X, e.Y);
TreeNode node = feedTreeView.GetNodeAt(point);You can then store
node
in a class level member variable, which you can then access from anywhere within the class. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
You can subscribe to the MouseDown event and figure out which node was right-clicked on with the help of the mouse coordinates.
Point point = new Point(e.X, e.Y);
TreeNode node = feedTreeView.GetNodeAt(point);You can then store
node
in a class level member variable, which you can then access from anywhere within the class. Regards Senthil _____________________________ My Blog | My Articles | WinMacroThanks for your suggestion. I'm new to OO and I think I was just over-complicating things! Regards SH