none
How to search treeNode object in TreeView? RRS feed

  • Question

  • Hi there,

    I have an issue with TreeView Form controler,  I would like to know if there is a way (and i'm sure there is) to search and "get" a trrenode from the treeView by the node name - string.

    My need is to add changes in existing treeView nodes that already exists and i  don' know the index of each node.

    I write in C# and have few knowledge in .NET forms.
    TIA
    Saturday, June 17, 2006 5:18 PM

Answers

  • Hi,
    this should do the job:

    TreeNode FindNameInTreeView(TreeNode node, String name)
    {
    // check if we have a node
    if (node == null)
    return null;

    // check if the name of this node matches the one we're looking for
    if (node.Name == name)
    return node;

    // iterate through the childs of this node
    for (int i = 0; i < node.Nodes.Count; i++)
    {
    // call this same function to each child node
    TreeNode foundNode = FindNameInTreeView(node.Nodes[ i ], name);
    // if we found the node, return it
    if (foundNode != null)
    return foundNode;
    }

    // no node found
    return null;
    }

    TreeNode FindNameInTreeView(TreeView treeView, String name)
    {
    // check if we have a treeview
    if (treeView == null)
    return null;

    // iterate through the treeview's root nodes
    for (int i = 0; i < treeView.Nodes.Count; i++)
    {
    // for each root node try to find the node with the name we want
    TreeNode foundNode = FindNameInTreeView(treeView.Nodes[ i ], name);
    // if we found the node, return it
    if (foundNode != null)
    return foundNode;
    }

    // no node found
    return null;
    }

    to make it work call:
    TreeNode node = FindNameInTreeView(treeView, "hi");
    if (node != null)
    MessageBox.Show("Node Found!");


    Sunday, June 18, 2006 12:21 AM

All replies

  • //iterate thorugh the TreeView Nodes


    for (int i = 0; i < this.treeView1.Nodes.Count; i++)
    {
                  // Get the Node Name -> this.treeView1.NodesIdea.Name
    }
    Saturday, June 17, 2006 6:01 PM
  • //iterate thorugh the TreeView Nodes


    for (int i = 0; i < this.treeView1.Nodes.Count; i++)
    {
                  // Get the Node Name -> this.treeView1.Nodes[ i ].Name
    }
    Saturday, June 17, 2006 6:03 PM
  • This solution works only on the first depth level.

    The tree structure is build with different depth, each node is in different level.

    Saturday, June 17, 2006 10:13 PM
  • You can go to any depth level, just by using the Nodes Property at evey Node like

    // This loop will iterate through depth level 01

    for (int i = 0; i < this.treeView1.Nodes.Count; i++)
    {
    // this.treeView1.Nodes[ i ].Name

    // To go further down, iterate through each node
    // this.treeView1.Nodes[ i ].Nodes.Count;
    }

    Hope this helps

    Saturday, June 17, 2006 10:23 PM
  • Hi,
    this should do the job:

    TreeNode FindNameInTreeView(TreeNode node, String name)
    {
    // check if we have a node
    if (node == null)
    return null;

    // check if the name of this node matches the one we're looking for
    if (node.Name == name)
    return node;

    // iterate through the childs of this node
    for (int i = 0; i < node.Nodes.Count; i++)
    {
    // call this same function to each child node
    TreeNode foundNode = FindNameInTreeView(node.Nodes[ i ], name);
    // if we found the node, return it
    if (foundNode != null)
    return foundNode;
    }

    // no node found
    return null;
    }

    TreeNode FindNameInTreeView(TreeView treeView, String name)
    {
    // check if we have a treeview
    if (treeView == null)
    return null;

    // iterate through the treeview's root nodes
    for (int i = 0; i < treeView.Nodes.Count; i++)
    {
    // for each root node try to find the node with the name we want
    TreeNode foundNode = FindNameInTreeView(treeView.Nodes[ i ], name);
    // if we found the node, return it
    if (foundNode != null)
    return foundNode;
    }

    // no node found
    return null;
    }

    to make it work call:
    TreeNode node = FindNameInTreeView(treeView, "hi");
    if (node != null)
    MessageBox.Show("Node Found!");


    Sunday, June 18, 2006 12:21 AM
  • The term is called recursion. You create a method that will take a node preferrably the root node.

    Traverse(parentnode)
    {
      for each node in parentnode
      {
         if node has children
            Traverse(node)
        else
            // process node
      }
    }

    Sunday, June 18, 2006 6:42 AM
  • Or even easier (assuming you're using 2.0 framework)...

         Nodes.Find("some node name", true);  //true for search children, false otherwise

    Returns an array of TreeNodes.

     

     

    Steve

     

    Tuesday, June 27, 2006 1:30 PM