Bonjour,

Je dois créer un programme permettant la visualisation d'associations entre deux TreeView remplis dynamiquement.
Pour cela, il me faut donc parcourir chacun des noeuds d'un TreeView et vérifier si il correspond au résultat de la requête, pour cela, je maîtrise la bonne méthode bien horrible et extrêmement peu fonctionnelle de l'imbrication en masse de For To Next...

Comme ceci :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
If oDataReader.HasRows Then
                Do While oDataReader.Read()
                    Dim strTemp As String = oDataReader("num_classification")
 
                    For i As Integer = 0 To Me.tvClassification.GetNodeCount(False) - 1
                        If String.Compare(Me.tvClassification.Nodes(i).Name, strTemp) = 0 Then
                            Me.tvClassification.Nodes(i).Expand()
                            Me.tvClassification.Nodes(i).BackColor = Color.Red
                            Me.tvClassification.Nodes(i).ForeColor = Color.White
                        End If
 
                        For j As Integer = 0 To Me.tvClassification.Nodes(i).GetNodeCount(False) - 1
                            If String.Compare(Me.tvClassification.Nodes(i).Nodes(j).Name, strTemp) = 0 Then
                                Me.tvClassification.Nodes(i).Expand()
                                Me.tvClassification.Nodes(i).Nodes(j).Expand()
                                Me.tvClassification.Nodes(i).Nodes(j).BackColor = Color.Red
                                Me.tvClassification.Nodes(i).Nodes(j).ForeColor = Color.White
                            End If
 
                            For k As Integer = 0 To Me.tvClassification.Nodes(i).Nodes(j).GetNodeCount(False) - 1
                                If String.Compare(Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Name, strTemp) = 0 Then
                                    Me.tvClassification.Nodes(i).Expand()
                                    Me.tvClassification.Nodes(i).Nodes(j).Expand()
                                    Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Expand()
                                    Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).BackColor = Color.Red
                                    Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).ForeColor = Color.White
                                End If
 
                                For l As Integer = 0 To Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).GetNodeCount(False) - 1
                                    If String.Compare(Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Name, strTemp) = 0 Then
                                        Me.tvClassification.Nodes(i).Expand()
                                        Me.tvClassification.Nodes(i).Nodes(j).Expand()
                                        Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Expand()
                                        Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Expand()
                                        Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).BackColor = Color.Red
                                        Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).ForeColor = Color.White
                                    End If
                                    For m As Integer = 0 To Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).GetNodeCount(False) - 1
                                        If String.Compare(Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Nodes(m).Name, strTemp) = 0 Then
                                            Me.tvClassification.Nodes(i).Expand()
                                            Me.tvClassification.Nodes(i).Nodes(j).Expand()
                                            Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Expand()
                                            Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Expand()
                                            Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Nodes(m).Expand()
                                            Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Nodes(m).BackColor = Color.Red
                                            Me.tvClassification.Nodes(i).Nodes(j).Nodes(k).Nodes(l).Nodes(m).ForeColor = Color.White
                                        End If
                                    Next
                                Next
                            Next
                        Next
                    Next
                Loop
            End If
J'ai déjà dit trouver ça horriblement peu fonctionnel?

J'aimerais remplacer ce code par des fonctions récursives du style :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Private Sub TestRecursive(ByVal tn As TreeNode, ByVal str As String)
        For Each tn In tn.Nodes
            If String.Compare(tn.Name, str) = 0 Then
                tn.Expand()
                tn.ForeColor = Color.White
                tn.BackColor = Color.Red
            End If
        Next
    End Sub
    Private Sub CallTestRecursive(ByVal tv As TreeView, ByVal str As String)
        Dim n As TreeNode
        For Each n In tv.Nodes
            TestRecursive(n, str)
        Next
    End Sub
Le code avec les "For" donne bien le résultat attendu, ce n'est pas le cas des mes fonctions récursives, je me demande donc d'ou peu venir le problème.
J'ai déjà réussi à utiliser la récursivité pour réinitialiser la couleur de chaque noeud de mes TreeView, mais dans ce cas, je heurte un mur...

Peut-être quelqu'un ayant mieux saisi ce concept de récursivité pourrait m'éclairer sur ce qui bloque mon avancée ?


Ah, peut-être avez vous besoin de ma requête, je l'ajoute au cas où :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
            Dim strTmp As String = tvMenu.SelectedNode.Name 'Contient "num_classification"
            Dim strRequete As String = "SELECT id_mainmenu, id_section, id_sous_section, MC.id_classification, num_classification " & _
                                       "FROM menus_classifications AS MC " & _
                                            "RIGHT OUTER JOIN classifications AS C " & _
                                            "ON MC.id_classification = C.id_classification " & _
                                       "WHERE id_mainmenu = '" & strTmp & "' " & _
                                            "OR (id_section = '" & strTmp & "') " & _
                                            "OR (id_sous_section = '" & strTmp & "')"
            Dim oCmdSQL As New OleDbCommand(strRequete, oConnection)
 
            Dim oDataReader As OleDbDataReader = oCmdSQL.ExecuteReader
Voilà, j'espère avoir fait preuve de clarté lors de mes explication, si certains points sont flou, je m'efforcerai d'éclaircir cela. Merci d'avacne à ceux qui auront le temps de m'aider