1

I want to get the node's keys just 'only in view' on treeview.

Here is the example;

enter image description here

I am using below code to get all node recursively. It just return all nodes key as expected. however i need to get the keys that only in treeview's view;

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

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

I don't know i should follow different path or just reorganize code.

I just stacked after many hours. Need your help.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
john true
  • 263
  • 1
  • 5
  • 26
  • 1
    Have you tried the `.IsExpanded`, `.IsVisible`, `.NextVisibleNode`, `.PrevVisibleNode` properties of the Treenode? (If I correctly understand the question). – Jimi Dec 16 '17 at 21:24
  • @Jimi hey, yes i tried next visible node and it works perfect but i didn't make it in for loop. How can i get all nodes using next visible node programmatically? – john true Dec 16 '17 at 21:35

1 Answers1

2

You can find the first visible node using Nodes collection and IsVisible property of the Node. Then create a recursive method which uses NextVisibleNode to find the next visible node in TreeView.

private void button1_Click(object sender, EventArgs e)
{
    var visibleNodes = GetVisibleNodes(treeView1).ToList();
}
public IEnumerable<TreeNode> GetVisibleNodes(TreeView t)
{
    var node = t.Nodes.Cast<TreeNode>().Where(x => x.IsVisible).FirstOrDefault();
    while (node != null)
    {
        var temp = node;
        node = node.NextVisibleNode;
        yield return temp;
    }
}

Also as another option, you can rely on Descendants extension method to flatten the TreeView and then using IsVisible property, get all visible nodes.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398