Results 1 to 3 of 3

Thread: [RESOLVED] Find Treeview node and parent by Tag

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2014
    Posts
    57

    Resolved [RESOLVED] Find Treeview node and parent by Tag

    I have a treeview which I put a bunch of Category Numbers in as the Tag of the node. Now after having that category number, in another application I wish to search the Category to get the parent text as well as the current selected text. My code looks like this thus far.

    Code:
            With TreeView1
                For Each node As TreeNode In .Nodes
                    If node.Tag = TextBoxCategory.Text Then
                        .SelectedNode = node
                        tbLexicon.Text = .SelectedNode.Parent.Text & " > " & .SelectedNode.Text
                        Exit For
                    End If
                Next
            End With
    This code came from MSDN, but it never finds my Tag which I know is in it as the other application supplies the values that I am sending back from the same treeview, just copied over to the other app. I have search for hours on this, but the results that I have tried just don't work or have errors that don't correct. For example... A sample of my treeview looks like this...

    Air Condition and Heating
    .........A/C Compressor & Clutch
    ..................Tag = 33543
    .........A/C Hoses & Fittings
    ..................Tag = 33544
    .........A/C & Heater Controls
    ..................Tag = 33545

    Any help much appreciated.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Find Treeview node and parent by Tag

    This is what I use....


    vb.net Code:
    1. Dim gotNode As Boolean = False
    2.     Private Sub selectTreeNode(ByVal parentNode As TreeNode, ByVal nodeID As String)
    3.         If Not gotNode Then
    4.             For Each node As TreeNode In parentNode.Nodes
    5.  
    6.                 If node.Tag IsNot Nothing AndAlso node.Tag.ToString = nodeID Then
    7.                     node.TreeView.SelectedNode = node
    8.                     gotNode = True
    9.  
    10.                     Exit Sub
    11.  
    12.                 Else
    13.  
    14.                     Call selectTreeNode(node, nodeID)
    15.  
    16.                 End If
    17.             Next
    18.         End If
    19.  
    20.     End Sub

    and to use it....

    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.  
    3.         gotNode = False
    4.         For Each n As TreeNode In Me.TreeView1.Nodes
    5.             If Not gotNode Then
    6.                 selectTreeNode(n, "This")
    7.  
    8.             End If
    9.         Next
    10.  
    11.     End Sub
    I would not be at all surprised to see someone post a LINQ solution as well.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2014
    Posts
    57

    Re: Find Treeview node and parent by Tag

    Thank you. It took me a minute to see what it was doing, but that worked great. Replaced "This" with TextBoxCategory.Text and filled tbLexicon by testing if gotNode was true.

Tags for this Thread

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