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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find several times the same node in a treeview

Status
Not open for further replies.

curiousvbnet

Programmer
Apr 6, 2007
40
FR
I have created a sub wich permits to find a node in a treeview.
A user clicks on an item in the ListView1 and the node which corresponds to this item's tag appears in color blue in the TreeView1
But if this same node exists several times in the treeview, how can i recover all the places in the treeview where
it is.

For the moment the sub code is this one

Code:
 [blue]Private Sub ListView1_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.ItemActivate[/blue]
        Dim iSelectedTermeID As Integer, strLibTerme As String, id_mt As Integer

        'déclaration de deux arrays qui vont contenir plusieurs lignes
        Dim drNAs() As DataRow, drSyns() As DataRow, drNA As DataRow, drSYN As DataRow
        Dim nNANode As TreeNode, nSynNode As TreeNode


        
        If Not ListView1.FocusedItem Is Nothing AndAlso Not ListView1.FocusedItem.Tag Is Nothing Then
           
            ListView1.FocusedItem.ForeColor = SystemColors.HighlightText.Magenta
            iSelectedTermeID = CType(ListView1.FocusedItem.Tag, Integer)
            strLibTerme = ListView1.SelectedItems(0).Text

          [green]  ' search the node in the TreeView1[/green]
            nNode1 = FindNode(iSelectedTermeID, TreeView1.Nodes)

        If Not nNode1 Is Nothing Then

              TreeView1.SelectedNode = nNode1

        If Not TreeView1.SelectedNode Is Nothing Then
                   [blue] With nNode1
                        .BackColor = SystemColors.Highlight.DeepSkyBlue
                        .ForeColor = SystemColors.HighlightText.MediumSlateBlue

                    End With[/blue]
                    nNode1.ExpandAll()

                   
                  End If ' fin de If Not TreeView1.SelectedNode Is Nothing

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

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

   [green]   the tag value is the one after the two letters "GT" ou "MT"[/green]
                       iTagVal = CType(strTag.Substring(2), Integer)
      'strTag est égale aux deux lettres "GT " ou "MT ", qui sont les deux premiers caractères du tag
      strTag = strTag.Substring(0, 2)

      If strTag = "GT" And iTagVal = iTermeID Then
        nFoundNode = nNode
        Exit For
        '             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]

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

        Return nFoundNode
  End Function

Thanks a lot for your help and advices.

Best regards.
Nathalie
 
I could be wrong saying this but maybe you could start by not Exiting the for loop so that you can check all the nodes.

Code:
If strTag = "GT" And iTagVal = iTermeID Then
        nFoundNode = nNode
        Exit For  '<---- il ne faut pas quitter la boucle si tu veux vérifier les autres TreeNodes
        '             Else


and then instead of returning only one node, your function could return a collection of nodes so that you can process all of the matching nodes.

The only problem is that I don't think the basic treeview accepts multiple nodes being selected simultaneously so you'll have to paint them yourself.

 
Hi,

You are absolutely right.

For this reason i have written this part of code

Code:
 With nNode1
                        .BackColor = SystemColors.Highlight.DeepSkyBlue
                        .ForeColor = SystemColors.HighlightText.MediumSlateBlue

                    End With

but i think i will have to loop also in the listitem activate sub until nNode1=nothing

I don't know if the EnsureVisuble property can be called also... i will try

Thanks a lot again.

Regards.
Nathalie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top