如何找到treeview的某节点的所有子节点(包括所有孙子节点)

解决方案 »

  1.   

    msdn上面的一个例子。Option Explicit
    Private Sub Form_Click()
       Dim strC As String
       Dim N As Integer
       If TreeView1.SelectedItem.Children > 0 Then ' There are children.      ' Get first child's text, and set N to its index value.
          strC = TreeView1.SelectedItem.Child.Text & vbLf
          N = TreeView1.SelectedItem.Child.Index      ' While N is not the index of the child node's
          ' last sibling, get next sibling's text.
          While N <> TreeView1.SelectedItem.Child.LastSibling.Index
             strC = strC & TreeView1.Nodes(N).Next.Text & vbLf
             ' Reset N to next sibling's index.
             N = TreeView1.Nodes(N).Next.Index
          Wend
          ' Show results.
          MsgBox "Children of " & TreeView1.SelectedItem.Text & _
          " are: " & vbLf & strC
       Else ' There are no children.
          MsgBox TreeView1.SelectedItem.Text & " has no children"
       End If
    End SubPrivate Sub Form_Load()
       TreeView1.BorderStyle = 1  ' Ensure border is visible
       Dim nodX As Node
       Set nodX = TreeView1.Nodes.Add(, , "d", "Dates")
       Set nodX = TreeView1.Nodes.Add("d", tvwChild, "d89", "1989")
       Set nodX = TreeView1.Nodes.Add("d", tvwChild, "d90", "1990")   ' Create children of 1989 node.
       Set nodX = TreeView1.Nodes.Add("d89", tvwChild, , "John")
       Set nodX = TreeView1.Nodes.Add("d89", tvwChild, , "Brent")
       Set nodX = TreeView1.Nodes.Add("d89", tvwChild, , "Eric")
       Set nodX = TreeView1.Nodes.Add("d89", tvwChild, , "Ian")
       nodX.EnsureVisible ' Show all nodes.   ' Create children of 1990 node.
       Set nodX = TreeView1.Nodes.Add("d90", tvwChild, , "Randy")
       Set nodX = TreeView1.Nodes.Add("d90", tvwChild, , "Ron")
       nodX.EnsureVisible ' Show all nodes.
    End Sub
      

  2.   

    使用递归算法,很简单Function SearchTxt(Nod As Node, txtInfo As String) As String
        If Nod.Text = txtInfo Then
           SearchTxt = Nod.Key
        ElseIf Nod.Children > 0 Then
           SearchTxt = SearchTxt(Nod.Child, txtInfo)
        ElseIf Not Nod.Next Is Nothing Then
           SearchTxt = SearchTxt(Nod.Next, txtInfo)
        End If
    End Function