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. TreeView node right-clicked is not the "selected" node...

TreeView node right-clicked is not the "selected" node...

Scheduled Pinned Locked Moved C#
data-structuresquestion
7 Posts 3 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.
  • R Offline
    R Offline
    rhoward
    wrote on last edited by
    #1

    Why is it that when I right click on a tree node and my context menu pops up, the item that is returned by the treeView1.SelectedNode property is not the item that was right-clicked on, but whatever tree node happend to be selected before the right click? i.e. Folders _________ |FolderA| <-- Currently selected tree node --------- FolderB FolderC <-- Right click here treeView1.SelectedNode returns FolderA, not FolderC, even thought the selected node highlighting box temporarily moved to FolderC. Scratching my head... Thanks, Rick

    J D 2 Replies Last reply
    0
    • R rhoward

      Why is it that when I right click on a tree node and my context menu pops up, the item that is returned by the treeView1.SelectedNode property is not the item that was right-clicked on, but whatever tree node happend to be selected before the right click? i.e. Folders _________ |FolderA| <-- Currently selected tree node --------- FolderB FolderC <-- Right click here treeView1.SelectedNode returns FolderA, not FolderC, even thought the selected node highlighting box temporarily moved to FolderC. Scratching my head... Thanks, Rick

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Thats just the way the TreeView works. Crappy answer, but if you look at the behavior in Windows Explorer it is the same. I believe the justification is that selecting the node is so you can tell which node was right-clicked on. Right clicking should not select an item; you are merely requesting a menu to popup. James

      R 1 Reply Last reply
      0
      • R rhoward

        Why is it that when I right click on a tree node and my context menu pops up, the item that is returned by the treeView1.SelectedNode property is not the item that was right-clicked on, but whatever tree node happend to be selected before the right click? i.e. Folders _________ |FolderA| <-- Currently selected tree node --------- FolderB FolderC <-- Right click here treeView1.SelectedNode returns FolderA, not FolderC, even thought the selected node highlighting box temporarily moved to FolderC. Scratching my head... Thanks, Rick

        D Offline
        D Offline
        David Wengier
        wrote on last edited by
        #3

        As James said, that is by design. The way to work around the design is like so:

        treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

        Call that before you pop-up the context menu -- David Wengier Sonork ID: 100.14177 - Ch00k

        R 1 Reply Last reply
        0
        • J James T Johnson

          Thats just the way the TreeView works. Crappy answer, but if you look at the behavior in Windows Explorer it is the same. I believe the justification is that selecting the node is so you can tell which node was right-clicked on. Right clicking should not select an item; you are merely requesting a menu to popup. James

          R Offline
          R Offline
          rhoward
          wrote on last edited by
          #4

          I have to disagree, at least w/respect to how Windows Explorer works. If I left click Folder "A" in explorer to select it, then right click on folder "B" and select "properties" I get a property page for folder "B", not folder "A". This is the behaviour on Windows 2000. If I were programming with pure Win32, I would just do a hit-test on the item under the mouse cursor, but I can't find an analog in C#.NET. Thanks, Rick

          J 1 Reply Last reply
          0
          • D David Wengier

            As James said, that is by design. The way to work around the design is like so:

            treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

            Call that before you pop-up the context menu -- David Wengier Sonork ID: 100.14177 - Ch00k

            R Offline
            R Offline
            rhoward
            wrote on last edited by
            #5

            Many thanks, David. I was able to use GetNodeAt() in the MouseUp event for the tree to reset the selected node to the proper item. Thanks again, Rick

            1 Reply Last reply
            0
            • R rhoward

              I have to disagree, at least w/respect to how Windows Explorer works. If I left click Folder "A" in explorer to select it, then right click on folder "B" and select "properties" I get a property page for folder "B", not folder "A". This is the behaviour on Windows 2000. If I were programming with pure Win32, I would just do a hit-test on the item under the mouse cursor, but I can't find an analog in C#.NET. Thanks, Rick

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              rhoward wrote: If I left click Folder "A" in explorer to select it, then right click on folder "B" and select "properties" I get a property page for folder "B", not folder "A". Correct, but when you close the property page the folder "B" *isn't* selected, folder "A" is. Sorry I didn't make that clear in my posting. James

              R 1 Reply Last reply
              0
              • J James T Johnson

                rhoward wrote: If I left click Folder "A" in explorer to select it, then right click on folder "B" and select "properties" I get a property page for folder "B", not folder "A". Correct, but when you close the property page the folder "B" *isn't* selected, folder "A" is. Sorry I didn't make that clear in my posting. James

                R Offline
                R Offline
                rhoward
                wrote on last edited by
                #7

                Sorry for the misunderstanding. You are definitely correct about "A" still being selected. The point I was making is that Explorer displays properties for "B", not "A", which is what I was trying to achieve. Thanks, Rick

                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