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. TreeStructure problem

TreeStructure problem

Scheduled Pinned Locked Moved C#
questionhelp
11 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.
  • T Offline
    T Offline
    tommazzo
    wrote on last edited by
    #1

    Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?

    S 2 Replies Last reply
    0
    • T tommazzo

      Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?

      S Offline
      S Offline
      Sebrell
      wrote on last edited by
      #2

      You need a reference to the TreeView, and to set a TreeViewEventHandler for its AfterSelect event. TreeViewEventArgs has a Node property that returns the selected TreeNode.

      1 Reply Last reply
      0
      • T tommazzo

        Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?

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

        Or, the other way around, you can inherit from TreeView, create its nodes dynamically, and set each node's Tag property with an appropriate nested class instance.

        T 1 Reply Last reply
        0
        • S Sebrell

          Or, the other way around, you can inherit from TreeView, create its nodes dynamically, and set each node's Tag property with an appropriate nested class instance.

          T Offline
          T Offline
          tommazzo
          wrote on last edited by
          #4

          Thanks a lot! That's exactly what I was looking for. Now I've discovered another problem: The TreeView apparently always selects one node, no matter where I click. I, however have a context menu, which has to react differently, depending on whether the user clicks on a node or in some empty space. So I tried to determine the node at the clicking position with the GetNodeAt() method. This weardily always returns null. Here's the code (tvAlbums is the TreeView and contMenu the contextMenu): private void tvAlbums_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { Point pos = new Point(e.X, e.Y); contMenu.Show(tvAlbums, pos); pos = tvAlbums.PointToClient(pos); if(tvAlbums.GetNodeAt(pos) != null) { /* a node was selected - adjust contMenu accordingly */} else { /* no node is selected - adjust contMenu accordingly */} } }

          S 1 Reply Last reply
          0
          • T tommazzo

            Thanks a lot! That's exactly what I was looking for. Now I've discovered another problem: The TreeView apparently always selects one node, no matter where I click. I, however have a context menu, which has to react differently, depending on whether the user clicks on a node or in some empty space. So I tried to determine the node at the clicking position with the GetNodeAt() method. This weardily always returns null. Here's the code (tvAlbums is the TreeView and contMenu the contextMenu): private void tvAlbums_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { Point pos = new Point(e.X, e.Y); contMenu.Show(tvAlbums, pos); pos = tvAlbums.PointToClient(pos); if(tvAlbums.GetNodeAt(pos) != null) { /* a node was selected - adjust contMenu accordingly */} else { /* no node is selected - adjust contMenu accordingly */} } }

            S Offline
            S Offline
            Sebrell
            wrote on last edited by
            #5

            Try checking the tvAlbums.SelectedNode property instead of calling GetNodeAt - does it give you the correct Node reference? whoops -- i mean "in addition to calling GetNodeAt"

            T 1 Reply Last reply
            0
            • S Sebrell

              Try checking the tvAlbums.SelectedNode property instead of calling GetNodeAt - does it give you the correct Node reference? whoops -- i mean "in addition to calling GetNodeAt"

              T Offline
              T Offline
              tommazzo
              wrote on last edited by
              #6

              tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.

              S T 3 Replies Last reply
              0
              • T tommazzo

                tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.

                S Offline
                S Offline
                Sebrell
                wrote on last edited by
                #7

                In that case, a call to tvAlbums.SelectedNode.Bounds.Contains(new Point(e.X, e.Y)) should tell you whether the node was clicked.

                1 Reply Last reply
                0
                • T tommazzo

                  tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.

                  S Offline
                  S Offline
                  Sebrell
                  wrote on last edited by
                  #8

                  I forgot to add that you might want to change the value of tvAlbums.FullRowSelect and see what effect that has.

                  1 Reply Last reply
                  0
                  • T tommazzo

                    tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.

                    T Offline
                    T Offline
                    tommazzo
                    wrote on last edited by
                    #9

                    I've just found a working solution, which for which I cannot find any logical reason though: When I create a point based on some EventArgs e, I first have to let the point go through a PointToClient operation and let it go through PointToScreen to make the GetNodeAt method to work properly: Point p = new Point(e.X, e.Y); p = tvAlbums.PointToClient(p); p = tvAlbums.PointToScreen(p); // now tvAlbums.GetNodeAt(p) will work properly Thanks once again for your help!

                    S 1 Reply Last reply
                    0
                    • T tommazzo

                      I've just found a working solution, which for which I cannot find any logical reason though: When I create a point based on some EventArgs e, I first have to let the point go through a PointToClient operation and let it go through PointToScreen to make the GetNodeAt method to work properly: Point p = new Point(e.X, e.Y); p = tvAlbums.PointToClient(p); p = tvAlbums.PointToScreen(p); // now tvAlbums.GetNodeAt(p) will work properly Thanks once again for your help!

                      S Offline
                      S Offline
                      Sebrell
                      wrote on last edited by
                      #10

                      you're very welcome. Also, I realize upon reading your last message why you need to convert the Point. The MouseEventArgs X and Y properties relate to screen co-ordinates. Controls use co-ordinates based on their parent control's ClientRectangle. The conversion forces the TreeView to determine the screen co-ordinates of its own ClientRectangle, which it otherwise has no need to know. I don't suppose you know a fast and reliable way to serialize rich text, do you?:)

                      T 1 Reply Last reply
                      0
                      • S Sebrell

                        you're very welcome. Also, I realize upon reading your last message why you need to convert the Point. The MouseEventArgs X and Y properties relate to screen co-ordinates. Controls use co-ordinates based on their parent control's ClientRectangle. The conversion forces the TreeView to determine the screen co-ordinates of its own ClientRectangle, which it otherwise has no need to know. I don't suppose you know a fast and reliable way to serialize rich text, do you?:)

                        T Offline
                        T Offline
                        tommazzo
                        wrote on last edited by
                        #11

                        >I don't suppose you know a fast and reliable way to serialize rich text, do you? Perhaps the .NET baseclasses provide a serializer for rich-text.

                        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