3 Replies - 348 Views - Last Post: 06 February 2013 - 05:57 PM Rate Topic: -----

#1 Chrisix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 13

Getting a String from a TreeView

Posted 06 February 2013 - 04:44 PM

Hello, I have been trying some code on how to get a String from a TreeView. The String would have all The TreeNodes with a "\n" in between each one. It is irrelevant which order the TreeNodes are in. Here is some of my code...
        private void button1_Click(object sender, EventArgs e)
        {
            String str = "";

            for (int i = 0; i < lstTiles.Nodes.Count; i++)
            {
                TreeNode node = lstTiles.Nodes[i];
                String addon = getStringFromNode(node);
                if (addon != "")
                {
                    str += "\n" + addon;
                }

                if (i != lstTiles.Nodes.Count - 1)
                {
                    str += "\n";
                }
            }
            Clipboard.SetText(str);
        }

        private String getStringFromNode(TreeNode nodes)
        {
            String str = "";

            for (int i = 0; i < nodes.Nodes.Count; i++)
            {
                TreeNode node = nodes.Nodes[i];
                str += node.Text;
                String addon = getStringFromNode(node);
                if (addon != "")
                {
                    str += "\n" + addon;
                }

                if (i != lstTiles.Nodes.Count - 1)
                {
                    str += "\n";
                }
            }

            return str;
        }


Any ideas why this isn't working?

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a String from a TreeView

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4436
  • View blog
  • Posts: 12,307
  • Joined: 18-April 07

Re: Getting a String from a TreeView

Posted 06 February 2013 - 05:27 PM

Well part of the problem is the line str += node.Text;. This is in a loop, so it will be appending nodes to str for each child at the same level and since it doesn't also add a "\n" it is going to have strings that contain no "\n"

Assume I have the following structure...

Root
|_ Child1
|  |_ Grandchild1
|_ Child2



If I pass Root to your function it is going to add Child1 to str with no newline. It is then going to go into Grandchild1 where it will add to a new str and return it back to Child1. Awesome, there is a carriage new line there. It then goes around the for loop and adds Child2 to str which already contains Child1\nGrandchild1 and that makes it Child1\nGrandchild1Child2.

See why that would be? So you need to take a second look at that line I highlighted.

:)
Was This Post Helpful? 2
  • +
  • -

#3 Chrisix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 13

Re: Getting a String from a TreeView

Posted 06 February 2013 - 05:49 PM

Could I use the str += node.Text + "\n" and then after the main loop is done remove the "\n" at the end so there is not a blank line at the end?
Was This Post Helpful? 0
  • +
  • -

#4 Chrisix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 13

Re: Getting a String from a TreeView

Posted 06 February 2013 - 05:57 PM

View PostMartyr2, on 06 February 2013 - 05:27 PM, said:

Well part of the problem is the line str += node.Text;. This is in a loop, so it will be appending nodes to str for each child at the same level and since it doesn't also add a "\n" it is going to have strings that contain no "\n"

Assume I have the following structure...

Root
|_ Child1
|  |_ Grandchild1
|_ Child2



If I pass Root to your function it is going to add Child1 to str with no newline. It is then going to go into Grandchild1 where it will add to a new str and return it back to Child1. Awesome, there is a carriage new line there. It then goes around the for loop and adds Child2 to str which already contains Child1\nGrandchild1 and that makes it Child1\nGrandchild1Child2.

See why that would be? So you need to take a second look at that line I highlighted.

:)/>


Thank you! That worked perfectly. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1