Results 1 to 5 of 5

Thread: [RESOLVED] Same text nodes in TreeView

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    [RESOLVED] Same text nodes in TreeView

    Hi,

    How can I find and delete same text nodes in the TreeView?

    Like this (must be delete which it is signed with red rectangle);

    Last edited by YGTMLT; Feb 27th, 2007 at 09:04 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Same text nodes in TreeView

    You're just going to have to traverse the entire tree. Add the text of each node to a list as you go. When you reach each node check whether its text is already in the list. If it is then delete the node, otherwise add the text to the list and continue. Traversing a tree is best done using recursion. Here's a skeleton to get you started:
    Code:
    Public Sub VisitAllNodes(ByVal nodes As TreeNodeCollection)
        For Each node As TreeNode In nodes
            MessageBox.Show(node.Text)
    
            'Call the method recursively.
            Me.VisitAllNodes(node.Nodes)
        Next node
    End Sub
    You'd start that going by passing it the Nodes collection of the root of your search, which would normally be the TreeView itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: Same text nodes in TreeView

    Hi jmcilhinney,

    fine to see you again.

    When I calling your code like this;
    Code:
    Call VisitAllNodes(TreeView1.Nodes)
    All of nodes are coming in msgbox and how can I collect all nodes and find the same texts and then delete it.

    Best Regards.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Same text nodes in TreeView

    The best way to handle this situation is Not to add duplicate nodes to your treeview in the first place.... When you add child nodes to a parent node, just check the parent node to see if it already contains that specific new node. If it does, don't add the new node to it.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: Same text nodes in TreeView

    yeah this way to be perfectly resolved the problem.

    Thank you very very much guys.

Posting Permissions

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



Click Here to Expand Forum to Full Width