Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Obtaining a reference to the TreeView item in a context menu

Obtaining a reference to the TreeView item in a context menu

Scheduled Pinned Locked Moved C#
helpquestionwindows-admin
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SerialHobbyist
    wrote on last edited by
    #1

    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?

    S 1 Reply Last reply
    0
    • S SerialHobbyist

      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?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S S Senthil Kumar

        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

        S Offline
        S Offline
        SerialHobbyist
        wrote on last edited by
        #3

        Thanks for your suggestion. I'm new to OO and I think I was just over-complicating things! Regards SH

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups