0

when my page opens it runs this method:

    private void PrintRecursive(TreeNode treeNode, string problemValue)
    {
        // Print the node.
        System.Diagnostics.Debug.WriteLine(treeNode.Text);

        if (treeNode.Value == problemValue.Split(':')[0])
        {
            treeNode.Checked = true;

            // Then expand the SelectedNode and ALL its parents until no parent found

            treeNode.Expand();
            while (treeNode.Parent != null)
            {
                treeNode = treeNode.Parent;
                treeNode.Expand();
            }

            if (problemValue.Contains(":"))
                treeNode.Text += problemValue.Split(':')[1];
            return;
        }
        // Print each node recursively.
        foreach (TreeNode tn in treeNode.ChildNodes)
        {
            PrintRecursive(tn, problemValue);
        }
    }

treeNode.Checked = true works fine!

treeNode.Expand() work great!

however treeNode.Text += problemValue.Split(':')[1]; does nothing!

the value of problemValue is "111:someproblem"

what am i doing wrong?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • well you are using the split method incorrectly here.. what are you expecting the results to be..?? – MethodMan Jan 11 '12 at 20:54
  • @DJKRAZE whats wrong with the method? – Alex Gordon Jan 11 '12 at 20:55
  • the split returns properly.. but I am wondering if you should have returned it into a string[] and then check the values of the TreeNode.Value.. you should not hard code the ordinal position use a for loop or foreach or Create an IEnumerable do you follow – MethodMan Jan 11 '12 at 21:02
  • @DJKRAZE thank you again for your assistance. can you show me how i would use ienumerable for this? – Alex Gordon Jan 11 '12 at 21:03

2 Answers2

1

You need to move

        if (problemValue.Contains(":"))
            treeNode.Text += problemValue.Split(':')[1];

above the while statement.

The problem is that while you are expanding the parents, you are updating the value of treeNode, so when you try to set the text of treeNode, you are actually at one of the parent nodes.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
1

If you are wanting to see how to do this check out this previous StackOverFlow Posting as well for additional ideas on how to Enumerate TreeNodes Implementing IEnumerable

if you are wanting to print TreeNodes Recursively look at this example

private void PrintRecursive(TreeNode treeNode)
{
   // Print the node.
   System.Diagnostics.Debug.WriteLine(treeNode.Text);
   MessageBox.Show(treeNode.Text);
   // Print each node recursively.
   foreach (TreeNode tn in treeNode.Nodes)
   {
      PrintRecursive(tn);
   }
}

// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
   // Print each node recursively.
   TreeNodeCollection nodes = treeView.Nodes;
   foreach (TreeNode n in nodes)
   {
      PrintRecursive(n);
   }
}

If you want to do this as IEmumerable try the following below

public static class Extensions
{
    public static IEnumerable<T> GetRecursively<T>(this IEnumerable collection,
        Func<T, IEnumerable> selector)
    {
        foreach (var item in collection.OfType<T>())
        {
            yield return item;

            IEnumerable<T> children = selector(item).GetRecursively(selector);
            foreach (var child in children)
            {
                yield return child;
            }
        }
    }
}

Here's an example of how to use it

TreeView view = new TreeView();

// ...

IEnumerable<TreeNode> nodes = view.Nodes.
    .GetRecursively<TreeNode>(item => item.Nodes);
Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • thats exactly what am i expect to see; however the .Text is not getting affected at all – Alex Gordon Jan 11 '12 at 20:59
  • thank you so much. the guy below answered my question. but i willl DEFINITELY use your IENUMERABLE code thank you again for your time and help!!!!!!!! – Alex Gordon Jan 11 '12 at 21:13
  • awesome .. it's hard to tell sometimes if the OP's answers have been fulfilled without seeing any UPVotes.. good luck – MethodMan Jan 11 '12 at 21:14