Treeview noeud enfant vba

nina71287

XLDnaute Occasionnel
bonsoir,

j'aimerais récupérer les parents d'un nœud enfant j'ai essayé d'adapter ce code mais je n'y arrive pas

Dim intChild As Integer, i As Integer

For i = 1 To monarbre.Nodes.Count

intChild = Val(monarbre.Nodes.Item(i).Children)

If intChild > 0 Then MsgBox monarbre.Nodes.Item(i).Text & " " & LTrim(Str(intChild))
Next
je sais qu'avec parent on peut recuperer mais on recupere que le premier parent sachant qu'un noeud peut avoir plusieur niveaux

merci pour votre aide

bonne soirée
 

nina71287

XLDnaute Occasionnel
Re : Treeview noeud enfant vba

sur tes conseils je suis partis comme cela mais si j'ai plus de 3 niveaux cava etre long a ecrire comment pourrai je lecrire de maniere plus automatique

Dim intChild As Integer, i As Integer
For i = 1 To monarbre.Nodes.Count
intChild = Val(monarbre.Nodes.Item(i).Children)
If intChild = 0 Then
MsgBox monarbre.Nodes.Item(i).Text & " " & LTrim(Str(intChild))
MsgBox monarbre.Nodes.Item(i).parent.Text
If Val(monarbre.Nodes.Item(i).parent.Children) <> 1 Then
MsgBox (monarbre.Nodes.Item(i).parent.parent.Text)
End If
If Val(monarbre.Nodes.Item(i).parent.parent.Children) <> 1 Then
MsgBox Val(monarbre.Nodes.Item(i).parent.parent.parent.Text)
End If
End If
Next

merci pour votre aide
 

Habitude

XLDnaute Accro
Re : Treeview noeud enfant vba

Essaie ca
Code:
Dim intChild As Integer, i As Integer
For i = 1 To monarbre.Nodes.Count
intChild = Val(monarbre.Nodes.Item(i).Children)
        
    If intChild = 0 Then
        MsgBox monarbre.Nodes.Item(i)
        Parent monarbre.Nodes.Item(i).Index
    End If
Next

Ici la fonction Parent qui est récursive

Code:
Function Parent(indice)
    If Not monarbre.Nodes.Item(indice).Parent Is Nothing Then
        MsgBox monarbre.Nodes.Item(indice).Parent.Text
    End If
End Function
 

nina71287

XLDnaute Occasionnel
Re : Treeview noeud enfant vba

je te joint un exemple car je ne peut pas laisser les vrais données merci pour ton (j'ai mis ton code dans le bouton)
 

Pièces jointes

  • exemple.xlsm
    33.4 KB · Affichages: 503
  • exemple.xlsm
    33.4 KB · Affichages: 513
  • exemple.xlsm
    33.4 KB · Affichages: 545

Habitude

XLDnaute Accro
Re : Treeview noeud enfant vba

Je suis sur version 2003 et tu as probablement une version plus récente.
Ce qui fait que j'ai des problèmes avec ton fichier

Bref tu peux te faire une boucle inverse.
Qui part du noeud qui n'a pas d'enfant. Donc un enfant.

Et tu remonte jusqua ce que que le parent soit Vide. Donc le dernier en haut
Voici le code avec une boucle simple

Code:
dim noeud as node
For i = 1 To monarbre.Nodes.Count
intChild = Val(monarbre.Nodes.Item(i).Children)
if intChild= 0  then' ici tu trouve l'enfant
       noeud= monarbre.Nodes.Item(i) 'tu affecte l'item de type nodes à un type node 
       do while not noeud.parent is Nothing 
              msgbox noeud.parent.text
              noeud= noeud.parent 'on remonte jusquau dernier parent
       loop
endif
next
 

nina71287

XLDnaute Occasionnel
Re : Treeview noeud enfant vba

merci pr ta reponse dsl pr le fichier c'etait une version 2007 voici la 2003 j'ai ajouter ton bout de code mais il n'accepte pas la variable noeud comme node il trouve nothing erreur 91 je continuerai à chercher demain merci pour ton bout de code cava m'aider avancer
 

Pièces jointes

  • exemple(2).xlsm.zip
    35.6 KB · Affichages: 356

Bebere

XLDnaute Barbatruc
Re : Treeview noeud enfant vba

bonjour Nina,Habitude
essaye ce code(voir 'changé)
Private Sub commandButton1_Click()
Dim noeud As node
For i = 1 To monarbre.Nodes.Count
intChild = Val(monarbre.Nodes.Item(i).Children)
If intChild = 0 Then ' ici tu trouve l'enfant
'changé
Set noeud = monarbre.Nodes.Item(i) '.Text 'tu affecte l'item de type nodes à un type node
Do While Not noeud.Parent Is Nothing
'changé
MsgBox noeud.Text
'changé
Set noeud = noeud.Parent 'on remonte jusquau dernier parent
Loop
End If
Next
End Sub
 

Statistiques des forums

Discussions
312 339
Messages
2 087 407
Membres
103 538
dernier inscrit
Mbolatiana Hyacinthe