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. object from treeview help needed

object from treeview help needed

Scheduled Pinned Locked Moved C#
questionhelp
9 Posts 3 Posters 1 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.
  • B Offline
    B Offline
    BDJones
    wrote on last edited by
    #1

    Within a loop, I am creating an unknown number of objects. The object has 3 properties. This object has a ToString override. Directly after that object is created, it is added to a list and added as a node, and the ToString override is the visible node text. So what is actually stored in the treeview (text or the object) and how do I retrieve the object/get at it's properties when the node is selected? Thank you.

    L P 2 Replies Last reply
    0
    • B BDJones

      Within a loop, I am creating an unknown number of objects. The object has 3 properties. This object has a ToString override. Directly after that object is created, it is added to a list and added as a node, and the ToString override is the visible node text. So what is actually stored in the treeview (text or the object) and how do I retrieve the object/get at it's properties when the node is selected? Thank you.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      What is in the TreeView is whatever you added to it, from the sound of it, instances of your little class. Those instances get stored in the Nodes collection, on which you can operate with a set of methods, and which you can index like an array. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      B 1 Reply Last reply
      0
      • L Luc Pattyn

        What is in the TreeView is whatever you added to it, from the sound of it, instances of your little class. Those instances get stored in the Nodes collection, on which you can operate with a set of methods, and which you can index like an array. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        B Offline
        B Offline
        BDJones
        wrote on last edited by
        #3

        Thank you. df is the name of the object created in the loop. when using: myNode = new TreeNode(); How do I add the object? Currently I think I'm only adding a string by using: myNode = new TreeNode(df.ToString()); Your comments are appreciated.

        L 1 Reply Last reply
        0
        • B BDJones

          Thank you. df is the name of the object created in the loop. when using: myNode = new TreeNode(); How do I add the object? Currently I think I'm only adding a string by using: myNode = new TreeNode(df.ToString()); Your comments are appreciated.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          as you seem to add just strings, all you've got in the tree is strings. drop the ToString() and the tree will look the same but hold real objects now. I think you should be able to figure that out yourself. BTW: the same would apply to other list oriented controls such as ComboBox, ListBox, ... :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          B 1 Reply Last reply
          0
          • L Luc Pattyn

            as you seem to add just strings, all you've got in the tree is strings. drop the ToString() and the tree will look the same but hold real objects now. I think you should be able to figure that out yourself. BTW: the same would apply to other list oriented controls such as ComboBox, ListBox, ... :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            B Offline
            B Offline
            BDJones
            wrote on last edited by
            #5

            I thought I could, but when using the object as the argument, I get an error myNode = new TreeNode(df); "Argument '1' cannot convert '...DetailFob' to 'String' " Sorry if this is so simple. I did try this before posting.

            L 1 Reply Last reply
            0
            • B BDJones

              I thought I could, but when using the object as the argument, I get an error myNode = new TreeNode(df); "Argument '1' cannot convert '...DetailFob' to 'String' " Sorry if this is so simple. I did try this before posting.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, sorry I was wrong (I use ListBox a lot, and TreeView very rarely). In ComboBox and ListBox, you can add arbitrary types as items to the list, and what you see is its ToString result (unless OwnerDraw is in effect); however in a TreeView the items have to be TreeNode instances (the tree needs a standard way to find its branches and leaves, a ComboBox/ListBox doesn't have any hierarchy) and what you see is the TreeNode.Text I see basically two ways to store more than just a string in a TreeView node: 1. create a TreeNode as you did, and store a reference to your object in the TreeNode.Tag property, which is there for you to use any way you like; you still have to provide a Text to the TreeNode (which you did), and if you want Find() to be successful, you should also set the Name property to the text value. 2. create a new class, say MyTreeNode, inheriting from TreeNode. You still need to explicitly set the Text property (as well as Name for a successful Find), but you no longer need to use Tag. As we don't have multiple inheritance nowadays, if you wanted to store say a Form in a tree (Form inherits from Control), you would have to use the first way (with Tag). The main difference between both methods is the Tag indirection: the first item would be myTree.Nodes[0].Tag in (1) and myTree.Nodes[0] itself in (2) :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              B 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, sorry I was wrong (I use ListBox a lot, and TreeView very rarely). In ComboBox and ListBox, you can add arbitrary types as items to the list, and what you see is its ToString result (unless OwnerDraw is in effect); however in a TreeView the items have to be TreeNode instances (the tree needs a standard way to find its branches and leaves, a ComboBox/ListBox doesn't have any hierarchy) and what you see is the TreeNode.Text I see basically two ways to store more than just a string in a TreeView node: 1. create a TreeNode as you did, and store a reference to your object in the TreeNode.Tag property, which is there for you to use any way you like; you still have to provide a Text to the TreeNode (which you did), and if you want Find() to be successful, you should also set the Name property to the text value. 2. create a new class, say MyTreeNode, inheriting from TreeNode. You still need to explicitly set the Text property (as well as Name for a successful Find), but you no longer need to use Tag. As we don't have multiple inheritance nowadays, if you wanted to store say a Form in a tree (Form inherits from Control), you would have to use the first way (with Tag). The main difference between both methods is the Tag indirection: the first item would be myTree.Nodes[0].Tag in (1) and myTree.Nodes[0] itself in (2) :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                B Offline
                B Offline
                BDJones
                wrote on last edited by
                #7

                Thank you very much for your time Luc. :thumbsup:

                L 1 Reply Last reply
                0
                • B BDJones

                  Thank you very much for your time Luc. :thumbsup:

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  you're welcome. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  1 Reply Last reply
                  0
                  • B BDJones

                    Within a loop, I am creating an unknown number of objects. The object has 3 properties. This object has a ToString override. Directly after that object is created, it is added to a list and added as a node, and the ToString override is the visible node text. So what is actually stored in the treeview (text or the object) and how do I retrieve the object/get at it's properties when the node is selected? Thank you.

                    P Offline
                    P Offline
                    puri keemti
                    wrote on last edited by
                    #9

                    Store the selected node into Treenode. After that you can get all properties of node through object of treenode.

                    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