Loop Treeview Nodes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J.B.

    Loop Treeview Nodes

    Hi

    Can anybody show me how to loop all treeview nodes ini vb.net. I've been working on this for days but still no result.
    I got a sample code in vb 6.0, this one works.

    Private Sub SaveNested(
    Dim TNode As Node
    Set TNode = TreeView1.Nodes (1).Roo

    While Not TNode Is Nothin
    If TNode.children > 0 The
    AddChildNodes(T Node
    End I
    Set TNode = TNode.Nex
    Wend
    End Su

    Private Sub AddChildNodes(B yVal TNode As Node
    Dim childNode As Nod
    Dim i As Intege
    Set childNode = TNode.Chil

    For i = 0 To TNode.children - 1
    If childNode.child ren > 0 The
    AddChildNodes(c hildNode
    End I
    Set childNode = childNode.Nex
    Nex
    End Su

    I tried to rewrite the code in vb.net, but then stuck in using treeview(.net) properties. Could advice me how to write this in vb.net
    Thank

    Yant

  • EricJ

    #2
    Re: Loop Treeview Nodes


    i am working w the infragistics tree but it should be the same
    and i'm not rally sure this is wat you want (a loop not doing anithing
    special?)

    private sub looplvl1()[color=blue]
    > Dim TNode As Node[/color]
    for each tnode in tree.nodes
    messagebox.show (tnode.text)
    loopother(tnode )
    next
    end sub

    private sub loopother(PNode As Node )
    Dim TNode As Node
    for each tnode in Pnode.nodes
    messagebox.show (tnode.text)

    next

    end sub

    "J.B." <anonymous@disc ussions.microso ft.com> wrote in message
    news:8B1C7125-E80C-4B7E-BC75-5F910EA3CFCA@mi crosoft.com...[color=blue]
    > Hi,
    >
    > Can anybody show me how to loop all treeview nodes ini vb.net. I've been[/color]
    working on this for days but still no result.[color=blue]
    > I got a sample code in vb 6.0, this one works.
    >
    > Private Sub SaveNested()
    > Dim TNode As Node
    > Set TNode = TreeView1.Nodes (1).Root
    >
    > While Not TNode Is Nothing
    > If TNode.children > 0 Then
    > AddChildNodes(T Node)
    > End If
    > Set TNode = TNode.Next
    > Wend
    > End Sub
    >
    > Private Sub AddChildNodes(B yVal TNode As Node)
    > Dim childNode As Node
    > Dim i As Integer
    > Set childNode = TNode.Child
    >
    > For i = 0 To TNode.children - 1
    > If childNode.child ren > 0 Then
    > AddChildNodes(c hildNode)
    > End If
    > Set childNode = childNode.Next
    > Next
    > End Sub
    >
    > I tried to rewrite the code in vb.net, but then stuck in using[/color]
    treeview(.net) properties. Could advice me how to write this in vb.net.[color=blue]
    > Thanks
    >
    >
    > Yanto
    >[/color]


    Comment

    • Robin Tucker

      #3
      Re: Loop Treeview Nodes

      When you say "loop all tree view nodes", I assume you mean you want to
      iterate through the entire tree. You need recursion for this.

      Public Sub Iterate ()

      Iterate_Aux ( theTree.Nodes ( 0 ) )

      End Sub

      ' --- This is a prefix iteration

      Public Sub Iterate_Aux ( byref theNode as TreeViewNode )

      DoSomethingWith TheNode (theNode)

      ' Now do something with the children of this node

      for each theChildNode as TreeViewNode in theNode.Nodes
      Iterate_Aux ( theChildNode )
      next

      End Sub

      ' --- This is a postfix iteration

      Public Sub Iterate_Aux ( byref theNode as TreeViewNode )

      ' Do something with the children first

      for each theChildNode as TreeViewNode in theNode.Nodes
      Iterate_Aux ( theChildNode )
      next

      ' Do something with the node.

      DoSomethingWith TheNode (theNode)

      End Sub

      "J.B." <anonymous@disc ussions.microso ft.com> wrote in message
      news:8B1C7125-E80C-4B7E-BC75-5F910EA3CFCA@mi crosoft.com...[color=blue]
      > Hi,
      >
      > Can anybody show me how to loop all treeview nodes ini vb.net. I've been[/color]
      working on this for days but still no result.[color=blue]
      > I got a sample code in vb 6.0, this one works.
      >
      > Private Sub SaveNested()
      > Dim TNode As Node
      > Set TNode = TreeView1.Nodes (1).Root
      >
      > While Not TNode Is Nothing
      > If TNode.children > 0 Then
      > AddChildNodes(T Node)
      > End If
      > Set TNode = TNode.Next
      > Wend
      > End Sub
      >
      > Private Sub AddChildNodes(B yVal TNode As Node)
      > Dim childNode As Node
      > Dim i As Integer
      > Set childNode = TNode.Child
      >
      > For i = 0 To TNode.children - 1
      > If childNode.child ren > 0 Then
      > AddChildNodes(c hildNode)
      > End If
      > Set childNode = childNode.Next
      > Next
      > End Sub
      >
      > I tried to rewrite the code in vb.net, but then stuck in using[/color]
      treeview(.net) properties. Could advice me how to write this in vb.net.[color=blue]
      > Thanks
      >
      >
      > Yanto
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Loop Treeview Nodes

        * "=?Utf-8?B?Si5CLg==?=" <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
        > Can anybody show me how to loop all treeview nodes ini vb.net. I've been working on this for days but still no result.
        > I got a sample code in vb 6.0, this one works.[/color]

        \\\
        RecurseNodes(Me .TreeView1.Node s)
        ..
        ..
        ..

        Private Function RecurseNodes(By Val Node As TreeNodeCollect ion)
        For Each tn As TreeNode In Node
        If tn.Text = "Foo" Then
        Me.TreeView1.Se lectedNode = tn
        Return
        End If
        RecurseNodes(tn .Nodes)
        Next tn
        End Function
        ///

        --
        Herfried K. Wagner [MVP]
        <URL:http://dotnet.mvps.org/>

        Comment

        • J.B.

          #5
          Re: Loop Treeview Nodes

          Thanks for all of the responds. It helps a lot

          Well, the loop was for a start. I need to save treeview's data into xml file. I'm still working on it

          Yant

          ----- EricJ wrote: ----


          i am working w the infragistics tree but it should be the sam
          and i'm not rally sure this is wat you want (a loop not doing anithin
          special?

          private sub looplvl1([color=blue]
          > Dim TNode As Nod[/color]
          for each tnode in tree.node
          messagebox.show (tnode.text
          loopother(tnode
          nex
          end su

          private sub loopother(PNode As Node
          Dim TNode As Nod
          for each tnode in Pnode.node
          messagebox.show (tnode.text

          nex

          end su

          Comment

          Working...