如何:循环访问 Windows 窗体 TreeView 控件的所有节点

更新:2007 年 11 月

为了对节点值执行某种运算,查看 Windows 窗体 TreeView 控件中的每个节点有时是很有用的。利用递归过程(C# 和 C++ 中为递归方法)可完成此操作,该过程循环访问每个树集合中的每个节点。

树视图中的每个 TreeNode 对象都具有可用于定位树视图的属性:FirstNodeLastNodeNextNodePrevNode 以及 ParentParent 属性值是当前节点的父节点。当前节点如果有子节点,则子节点将列在它的 Nodes 属性中。TreeView 控件本身具有 TopNode 属性,该属性是整个树视图的根节点。

循环访问 TreeView 控件的所有节点

  1. 创建测试每个节点的递归过程(C# 和 C++ 中为递归方法)。

  2. 调用该过程。

    下面的示例演示如何打印每个 TreeNode 对象的 Text 属性:

    Private Sub PrintRecursive(ByVal n As TreeNode)
       System.Diagnostics.Debug.WriteLine(n.Text)
       MessageBox.Show(n.Text)
       Dim aNode As TreeNode
       For Each aNode In n.Nodes
          PrintRecursive(aNode)
       Next
    End Sub
    
    ' Call the procedure using the top nodes of the treeview.
    Private Sub CallRecursive(ByVal aTreeView As TreeView)
       Dim n As TreeNode
       For Each n In aTreeView.Nodes
          PrintRecursive(n)
       Next
    End Sub
    
    private void PrintRecursive(TreeNode treeNode)
    {
       // Print the node.
       System.Diagnostics.Debug.WriteLine(treeNode.Text);
       MessageBox.Show(treeNode.Text);
       // Print each node recursively.
       foreach (TreeNode tn in treeNode.Nodes)
       {
          PrintRecursive(tn);
       }
    }
    
    // Call the procedure using the TreeView.
    private void CallRecursive(TreeView treeView)
    {
       // Print each node recursively.
       TreeNodeCollection nodes = treeView.Nodes;
       foreach (TreeNode n in nodes)
       {
          PrintRecursive(n);
       }
    }
    
    private void PrintRecursive(TreeNode treeNode) 
    {
       // Print the node.
       System.Diagnostics.Debug.WriteLine(treeNode.get_Text());
       MessageBox.Show(treeNode.get_Text());
       for(int i = 0; i < treeNode.GetNodeCount(false); ++i) // Print each node recursively.
       {
          PrintRecursive(treeNode.get_Nodes().get_Item(i));
       }
    }
    
    // Call the procedure using the TreeView.
    private void CallRecursive(TreeView treeView) 
    {
       // Print each node recursively.
       TreeNodeCollection nodes = treeView.get_Nodes();
       for(int i = 0; i < nodes.get_Count(); ++i)
       {
          PrintRecursive(nodes.get_Item(i));
       }
    }
    
    private:
       void PrintRecursive( TreeNode^ treeNode )
       {
          // Print the node.
          System::Diagnostics::Debug::WriteLine( treeNode->Text );
          MessageBox::Show( treeNode->Text );
    
          // Print each node recursively.
          System::Collections::IEnumerator^ myNodes = (safe_cast<System::Collections::IEnumerable^>(treeNode->Nodes))->GetEnumerator();
          try
          {
             while ( myNodes->MoveNext() )
             {
                TreeNode^ tn = safe_cast<TreeNode^>(myNodes->Current);
                PrintRecursive( tn );
             }
          }
          finally
          {
             IDisposable^ disposable = dynamic_cast<System::IDisposable^>(myNodes);
             if ( disposable != nullptr )
                      disposable->Dispose();
          }
       }
    
       // Call the procedure using the TreeView.
       void CallRecursive( TreeView^ treeView )
       {
          // Print each node recursively.
          TreeNodeCollection^ nodes = treeView->Nodes;
          System::Collections::IEnumerator^ myNodes = (safe_cast<System::Collections::IEnumerable^>(nodes))->GetEnumerator();
          try
          {
             while ( myNodes->MoveNext() )
             {
                TreeNode^ n = safe_cast<TreeNode^>(myNodes->Current);
                PrintRecursive( n );
             }
          }
          finally
          {
             IDisposable^ disposable = dynamic_cast<System::IDisposable^>(myNodes);
             if ( disposable != nullptr )
                      disposable->Dispose();
          }
       }
    

请参见

概念

递归过程

其他资源

TreeView 控件(Windows 窗体)