TreeNodes search

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan T

    TreeNodes search

    I want to search the whole tree of a root node if one node has a tag value
    is matched.

    I have added tree nodes into a tree view with tag value is assigned to each
    node.

    If I want to find out the node has tag value say 10, how do I search thro'
    the whole tree ?


  • Marcin Hoppe

    #2
    Re: TreeNodes search

    Alan T wrote:
    I want to search the whole tree of a root node if one node has a tag value
    is matched.
    >
    I have added tree nodes into a tree view with tag value is assigned to each
    node.
    >
    If I want to find out the node has tag value say 10, how do I search thro'
    the whole tree ?
    The simplest idea is to search the tree recursively:

    private TreeNode SearchNodes(Tre eNodeCollection nodes)
    {
    TreeNode result = null;

    foreach(TreeNod e node in nodes)
    {
    // Here check the search condition.
    // Sample:
    if(node.Tag.ToS tring() == "10")
    {
    result = node;
    }
    else
    {
    result = SearchNodes(nod e.Nodes);
    }

    if(result != null)
    {
    break;
    }
    }

    return result;
    }

    private TreeNode SearchTreeView( TreeView view)
    {
    return SearchNodes(vie w.Nodes);
    }

    Best regards!
    Marcin

    Comment

    • Tom Spink

      #3
      Re: TreeNodes search

      Alan T wrote:
      I want to search the whole tree of a root node if one node has a tag value
      is matched.
      >
      I have added tree nodes into a tree view with tag value is assigned to
      each node.
      >
      If I want to find out the node has tag value say 10, how do I search thro'
      the whole tree ?
      Hi Alan,

      You need a recursive algorithm:

      ///
      private TreeNode FindNodeWithTag ( TreeNodeCollect ion nodes, object tag )
      {
      foreach ( TreeNode node in nodes )
      {
      if ( node.Tag == tag )
      return node;

      TreeNode candidate = FindNodeWithTag ( node.Nodes, tag );

      if ( candidate != null )
      return candidate;
      }

      return null;
      }
      ///

      Then, you call it by passing in the collection of nodes from your treeview,
      and the tag you want to search for. It will return the first node that
      matches that tag.

      --
      Hope this helps,
      Tom Spink

      Comment

      • Alan T

        #4
        Re: TreeNodes search

        Hi I got an exception here:

        private TreeNode SearchNode(Tree NodeCollection aNodes, int aId)

        {

        TreeNode result = null;


        foreach ( TreeNode node in aNodes )

        {

        if (node.Tag.ToStr ing() == aId.ToString()) <--- exception

        {

        return node;

        }

        The exception is "The object reference is not set to an instance of object"


        The simplest idea is to search the tree recursively:
        >
        private TreeNode SearchNodes(Tre eNodeCollection nodes)
        {
        TreeNode result = null;
        >
        foreach(TreeNod e node in nodes)
        {
        // Here check the search condition.
        // Sample:
        if(node.Tag.ToS tring() == "10")
        {
        result = node;
        }
        else
        {
        result = SearchNodes(nod e.Nodes);
        }
        >
        if(result != null)
        {
        break;
        }
        }
        >
        return result;
        }
        >
        private TreeNode SearchTreeView( TreeView view)
        {
        return SearchNodes(vie w.Nodes);
        }
        >
        Best regards!
        Marcin

        Comment

        • Marcin Hoppe

          #5
          Re: TreeNodes search

          Alan T wrote:
          if (node.Tag.ToStr ing() == aId.ToString()) <--- exception
          >
          The exception is "The object reference is not set to an instance of object"
          Apparently your nodes have the Tag property set to null. I assumed that
          you use it to store some information. If you store information in the
          Tag property of only some of your nodes, just check it before the
          comparison:

          if(node.Tag != null)
          {
          if(node.Tag.ToS tring() == aId.ToString())
          {
          return node;
          }
          }

          Best regards!
          Marcin

          Comment

          Working...