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 Select Problem

Treeview Node Select Problem

Scheduled Pinned Locked Moved C#
helpquestion
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
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear All, I have a treeview with several parent and child node. If I click on a parent, I want to invoke a method. If I click on a child , I want to invoke another method. The same method for the parents , and the same method for the childs. + Parent Node 1 - Child node 1 - Child node 1 + Parent Node 2 - Child node 1 This is the code I dreamed to achieve this. But .. for some reason it doesn't work. If I click on the child, it works .. however, the parent method is not fired when I click on the parent. Can somebody point me in the right direction with whatever it is that I'm doing wrong ? private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Parent.IsSelected) { MessageBox.Show("This is the parent node"); } else { MessageBox.Show("This is a child node"); } } Thanks :)

    C A 2 Replies Last reply
    0
    • R Rick van Woudenberg

      Dear All, I have a treeview with several parent and child node. If I click on a parent, I want to invoke a method. If I click on a child , I want to invoke another method. The same method for the parents , and the same method for the childs. + Parent Node 1 - Child node 1 - Child node 1 + Parent Node 2 - Child node 1 This is the code I dreamed to achieve this. But .. for some reason it doesn't work. If I click on the child, it works .. however, the parent method is not fired when I click on the parent. Can somebody point me in the right direction with whatever it is that I'm doing wrong ? private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Parent.IsSelected) { MessageBox.Show("This is the parent node"); } else { MessageBox.Show("This is a child node"); } } Thanks :)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Do you mean the after select event is not fired, or do you mean that the wrong message box shows ? I'd probably set the tree view items up with tags so you can tell what level they are on. Or check if the node has children, if the tree is only ever one level.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      R 1 Reply Last reply
      0
      • C Christian Graus

        Do you mean the after select event is not fired, or do you mean that the wrong message box shows ? I'd probably set the tree view items up with tags so you can tell what level they are on. Or check if the node has children, if the tree is only ever one level.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        R Offline
        R Offline
        Rick van Woudenberg
        wrote on last edited by
        #3

        Christian, Thank you for replying so quickly. The Afterselect is actually being called, and the second Messagebox ( when selecting a child ) comes up correctly .. but the first one isn't. Selecting a child with my code works fine, but not the parent. To me , that doesn't make sense, as it really checks if the parent is selected or not, or else it wouldn't make it to the else section and show the child messagebox. Rick

        C 1 Reply Last reply
        0
        • R Rick van Woudenberg

          Christian, Thank you for replying so quickly. The Afterselect is actually being called, and the second Messagebox ( when selecting a child ) comes up correctly .. but the first one isn't. Selecting a child with my code works fine, but not the parent. To me , that doesn't make sense, as it really checks if the parent is selected or not, or else it wouldn't make it to the else section and show the child messagebox. Rick

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Obviously this means the check is not doing what you expect it to. So, set a break point and look at the properties of the event args. As I said, I think that checking if the selected node has children looks like the easiest way to do what you're after.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          R 2 Replies Last reply
          0
          • C Christian Graus

            Obviously this means the check is not doing what you expect it to. So, set a break point and look at the properties of the event args. As I said, I think that checking if the selected node has children looks like the easiest way to do what you're after.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            R Offline
            R Offline
            Rick van Woudenberg
            wrote on last edited by
            #5

            Christian , I guess your right. I changed a bit in my code , and now it works fine :" private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Level == 0) ClearAll(); else FillDaTree(); } I'm very curious to know what the diff is between e.Node.Parent.IsSelected and e.Node.Level == 0 Cheers, Rick

            1 Reply Last reply
            0
            • C Christian Graus

              Obviously this means the check is not doing what you expect it to. So, set a break point and look at the properties of the event args. As I said, I think that checking if the selected node has children looks like the easiest way to do what you're after.

              Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              R Offline
              R Offline
              Rick van Woudenberg
              wrote on last edited by
              #6

              Well, I do know that difference as e.Node.Parent.IsSelected would only go up until it reached the parent of the child node, which could several levels deep, while e.Node.Level == 0 would got to the actual root node at level 0. But in a treeview with a parent at level 0 , and a child at level 1 , should just work.

              1 Reply Last reply
              0
              • R Rick van Woudenberg

                Dear All, I have a treeview with several parent and child node. If I click on a parent, I want to invoke a method. If I click on a child , I want to invoke another method. The same method for the parents , and the same method for the childs. + Parent Node 1 - Child node 1 - Child node 1 + Parent Node 2 - Child node 1 This is the code I dreamed to achieve this. But .. for some reason it doesn't work. If I click on the child, it works .. however, the parent method is not fired when I click on the parent. Can somebody point me in the right direction with whatever it is that I'm doing wrong ? private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Parent.IsSelected) { MessageBox.Show("This is the parent node"); } else { MessageBox.Show("This is a child node"); } } Thanks :)

                A Offline
                A Offline
                Alan N
                wrote on last edited by
                #7

                Hi Rick

                if (e.Node.Parent.IsSelected)
                {
                MessageBox.Show("This is the parent node");
                }

                This piece of code doesn't make sense in the context of an AfterSelect handler. By definition e.Node is selected therefore it's parent, e.Node.Parent, is not and e.Node.Parent.Selected will always be false. AlanN

                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