Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to recover nodes in a treeview

Status
Not open for further replies.

curiousvbnet

Programmer
Apr 6, 2007
40
FR
Hi,

i have created a sub which permits to recover a same node in a treeiew if this one exists at several places in the treeview.
By this sub i can recover the number of times this nodes exists in the treeview but not the nodes themselves .
How can i recover all the nodes of this same node.

for example : if i search the term 'search organism' in a treeview called treeview1 , this term exists at several places in the treeview but at each place its parent node is not the same.
So how can find all the nodes of this term.

Here is the code i use until now


Code:
[blue]Private Function Findnodescountintreeview(ByVal TagNode As String, ByVal Nodes As TreeNodeCollection) As Integer [/blue]
        Dim nNode As TreeNode, nResult As TreeNode 
        Dim nFoundNode As TreeNode = Nothing 
        Dim strTag As String, strTag_pref As String, iTagVal As Integer 
        Dim compte_noeud_cherche As Integer = 0 

      [green] 'Check to see if a node with the specified Tag exists in the current Nodes collection [/green]
        For Each nNode In Nodes 
          strTag = CType(nNode.Tag, String) 
            

         [green] 'the iTagVal value is the one after the two letters 'GT' or 'MT' [/green]
            iTagVal = CType(strTag.Substring(2), Integer) 
                                 
            If nNode.Tag = strTag Then 
                nFoundNode = nNode 

                compte_noeud_cherche = compte_noeud_cherche + 1 

               
                 
            Else 
              [green]  ' If we haven't found the node we were looking at check the nodes collection of all the Nodes in the current collection... [/green]

                compte_noeud_cherche = nodescountintreeview(TagNode, nNode.Nodes) 

            End If 
        Next 
        If Not nFoundNode Is Nothing Then 

            Return compte_noeud_cherche 
        End If 'fin de If Not nFoundNode Is Nothing 



    End Function 


Hereis the sub i use until now to fin a node which exists only once in the treeview 

[code]

[blue]Private Function FindNode(ByVal iTermeID As Integer, ByVal Nodes As TreeNodeCollection) As TreeNode [/blue]
    Dim nNode As TreeNode, nResult As TreeNode 
    Dim nFoundNode As TreeNode = Nothing 
    Dim strTag As String, iTagVal As Integer 

  [green]  'Check to see if a node with the specified Tag exists in the current Nodes collection [/green]
    For Each nNode In Nodes 
            strTag = CType(nNode.Tag, String) 

      'la valeur du tag est celle qui suit les deux caractères "GT" ou "MT" 
                       iTagVal = CType(strTag.Substring(2), Integer) 
      'strTag value is  "GT " or "MT ", whch are the two first characters of the tag 
      strTag = strTag.Substring(0, 2) 

      If strTag = "GT" And iTagVal = iTermeID Then 
        nFoundNode = nNode 
        Exit For 
        '         [green]        ' If we haven't found the node we were looking at check the nodes collection of all the Nodes in the current collection... [/green]

                nFoundNode = FindNode(iTermeID, nNode.Nodes) 
                If Not nFoundNode Is Nothing Then Exit For 
            End If 
        Next 

        Return nFoundNode 
  End Function 


[code]

Thanks a lot for your help. 

Best Regards

Nathalie
 
I would suggest defining a collection object before your loop. Then each time you identify a node you wish to return you can add it to your collection. At the end, return the collection.

I do not have extensive experience with collections in .Net, so there may be some subtleties to this that others can suggest, but I tested a very basic implementation and it worked. It appears, to my surprise, that Item property of the resulting collection is 1 based. Thus the code that showed it was working looked like this.
Code:
For int = 1 To 5
   MessageBox.Show(CType(colNodes.Item(int), TreeNode).Tag)
Next

When I tried starting int at zero, it got an error. Good luck.

Charlie
 
Hi Nathalie,

You can implement this kind of sub:

Code:
    Private Sub GetMatchingNodes(ByVal TagNode As String, ByVal Nodes As TreeNodeCollection, ByVal MatchingNodes As TreeNodeCollection)

        Dim node As TreeNode
        For Each node In Nodes

            'replace this condition with yours
            If node.Tag = TagNode Then

                'ok there's a match!
                'so we add the node to MatchingNodes
                MatchingNodes.Add(node)

            End If

            ' If the node has nodes then check them too
            If node.Nodes.Count > 0 Then
                GetMatchingNodes(TagNode, node.Nodes, MatchingNodes)
            End If

        Next

    End Sub


In order to use it:

Code:
        'instanciate the result collection
        Dim matchingNodes As New Collection
        'check your treeview
        GetMatchingNodes("search organism", TreeView1.Nodes, matchingNodes)

        'You want to know how many nodes matched your search?
        Dim nbResults As Integer = matchingNodes.Count

        'You wish to process those nodes in a specific way (custom paint etc..)
        For Each node As TreeNode In matchingNodes
            'example ...
            node.BackColor = Color.DeepSkyBlue
            node.ForeColor = Color.MediumSlateBlue
        Next

I hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top