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. Context Menu in TreeView control

Context Menu in TreeView control

Scheduled Pinned Locked Moved C#
windows-admintutorialquestion
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'm displaying a TreeView of an active directory domain. I want to right-click on a Node and display a context menu which has a submenu which is built depending on the type of object clicked on. I've created a context menu but when right-clicking on an object, this code, running in the Popup event of the context menu item which displays the sub-menu: MenuItem directoryObjectClickedOn = (MenuItem)sender; ContextMenu cm = directoryObjectClickedOn.GetContextMenu(); TreeView ad = (TreeView)cm.SourceControl; DirectoryEntry aDObject = (DirectoryEntry)ad.SelectedNode.Tag; MessageBox.Show(aDObject.Name); always gives me the previous object, not the current one. Any idea how to get the current object?

    A 1 Reply Last reply
    0
    • S SerialHobbyist

      I'm displaying a TreeView of an active directory domain. I want to right-click on a Node and display a context menu which has a submenu which is built depending on the type of object clicked on. I've created a context menu but when right-clicking on an object, this code, running in the Popup event of the context menu item which displays the sub-menu: MenuItem directoryObjectClickedOn = (MenuItem)sender; ContextMenu cm = directoryObjectClickedOn.GetContextMenu(); TreeView ad = (TreeView)cm.SourceControl; DirectoryEntry aDObject = (DirectoryEntry)ad.SelectedNode.Tag; MessageBox.Show(aDObject.Name); always gives me the previous object, not the current one. Any idea how to get the current object?

      A Offline
      A Offline
      Andy Moore
      wrote on last edited by
      #2

      In the MouseDown/Up event, get the x and y coordinates of the cursor. From this you can determine which node the mouse is on by using the GetNodeAt() method. If you want to know if a mouse has selected the tree view item label or its icon. I hope this helps. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

      S 1 Reply Last reply
      0
      • A Andy Moore

        In the MouseDown/Up event, get the x and y coordinates of the cursor. From this you can determine which node the mouse is on by using the GetNodeAt() method. If you want to know if a mouse has selected the tree view item label or its icon. I hope this helps. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

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

        Thanks very much. That sorted it out for me and I'm generating a context-specific menu.

        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