TreeView, the last node....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: TreeView, the last node....

  1. #1
    Join Date
    Jul 2002
    Posts
    146

    Angry TreeView, the last node....

    Dear all,
    can you indicate me how can I get the last node in a TreeView control?



    Many Thanks.

    Francesco

  2. #2
    Join Date
    May 2003
    Location
    Germany
    Posts
    936
    What is the last node for you? A TreeView contains a lot of Nodes and SubNodes and so on. Where is the begin and where is the end for you?

  3. #3
    Join Date
    May 2010
    Posts
    2

    Smile Re: TreeView, the last node....

    I know this post is very old, but I have a same problem.
    I would like recuperate the last node entered in the treeview in c#.
    I haven't found method and I don't know if it exists.
    Thanks for your help

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: TreeView, the last node....

    Quote Originally Posted by thomasg943 View Post
    I know this post is very old, but I have a same problem.
    I would like recuperate the last node entered in the treeview in c#.
    I haven't found method and I don't know if it exists.
    Thanks for your help
    I have no idea what you mean when you say 'recuperate', but getting a reference to the last node is pretty simple. You don't specify if you want child nodes and such, so I will assume you mean the last top level node.

    Code:
    TreeNode lastNode = treeView1.Nodes[ treeView1.Nodes.Count - 1 ];
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    May 2010
    Posts
    2

    Re: TreeView, the last node....

    I'm sorry for my "english", i'am french.
    I want the last node that I added in my treeview.
    Thanks for your answer.
    Thomas

  6. #6
    Join Date
    Jun 2010
    Posts
    1

    Smile Re: TreeView, the last node....

    Hello Dear Thomas,
    you can search the node that you currently added to treeview, if the node.Text is unique then search by text and if not , you can set some data into treeNode.Tag and use that data to find the node.

    here is a method that you may use to find every Node you want "FindByTag"

    private void FindByTag(string strSearch)
    {
    TreeNodeCollection nodes = tree_1.Nodes;
    foreach (TreeNode n in nodes)
    {
    if (((yourData)n.Tag).fieldName == strSearch)
    {
    n.BackColor = Color.Yellow;
    tree_1.SelectedNode = n;
    n.EnsureVisible();
    tree_1.SelectedNode = n;
    return;
    }
    FindRecursiveTag(n,strSearch);
    }
    }
    -----------------------------------

    private void FindRecursiveTag(TreeNode treeNode, string strSearch)
    {
    foreach (TreeNode tn in treeNode.Nodes)
    {
    if (((yourData)tn.Tag). fieldName == strSearch)
    {
    tn.BackColor = Color.Yellow;
    tree_1.SelectedNode = tn;
    tn.EnsureVisible();
    tree_1.SelectedNode = tn;
    return;
    }
    FindRecursiveTag(tn,strSearch);
    }
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    Re: TreeView, the last node....

    Quote Originally Posted by thomasg943 View Post
    I'm sorry for my "english", i'am french.
    I want the last node that I added in my treeview.
    Thanks for your answer.
    Thomas
    Why don't you keep track of the nodes as you add them to the tree? Just use a lastNode variable and overwrite it with the last node you've added. When you get done, adding nodes to the tree, the lastNode will contain the last node that was added.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center