1

I have 1 root node and many child nodes of that root node.

I want to get all of the visible nodes key.

The recursive code block like below;

public void PrintNodesRecursive(UltraTreeNode oParentNode)
{  
    foreach (UltraTreeNode oSubNode in ultraTree1.Nodes[0].Nodes)
    {
        MessageBox.Show(oSubNode.Key.ToString());
        PrintNodesRecursive(oSubNode);
    }             
}

private void ultraButton3_Click(object sender, EventArgs e)
{
    PrintNodesRecursive(ultraTree1.Nodes[0]);
}

However messagebox always show me '1' value. It doesn't count and endless loop happens.

How can I make it happen?

Danielle
  • 472
  • 4
  • 20
john true
  • 263
  • 1
  • 5
  • 26

2 Answers2

1

Try like this;

    public void PrintNodesRecursive(UltraTreeNode oParentNode)
    {
        if (oParentNode.Nodes.Length == 0)
        {
            return;
        }
        foreach (UltraTreeNode oSubNode in oParentNode.Nodes)
        {
            if(oSubNode.Visible)
            {
                MessageBox.Show(oSubNode.Key.ToString());
            }
            PrintNodesRecursive(oSubNode);
        }
    }

Also, put the visible condition in the loop.

lucky
  • 12,734
  • 4
  • 24
  • 46
1

You made a simple programming error. This line:

foreach (UltraTreeNode oSubNode in ultraTree1.Nodes[0].Nodes)

should probably be

foreach (UltraTreeNode oSubNode in oParentNode.Nodes)

Otherwise, every recursion step starts again from the top.

Heinzi
  • 167,459
  • 57
  • 363
  • 519