TreeNodeCollection.AddRange 方法

将先前创建的一组树节点添加到集合中。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable Sub AddRange ( _
    nodes As TreeNode() _
)
用法
Dim instance As TreeNodeCollection
Dim nodes As TreeNode()

instance.AddRange(nodes)
public virtual void AddRange (
    TreeNode[] nodes
)
public:
virtual void AddRange (
    array<TreeNode^>^ nodes
)
public void AddRange (
    TreeNode[] nodes
)
public function AddRange (
    nodes : TreeNode[]
)

参数

  • nodes
    表示要添加到集合中的树节点的 TreeNode 对象数组。

异常

异常类型 条件

ArgumentNullException

nodes 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentException

nodes 是另一 TreeView 的子级。

备注

包含在 nodes 数组中的 TreeNode 对象将附加到集合的末尾。

可以使用 AddRange 方法将一组先前创建的 TreeNode 对象迅速添加到集合中,而不是通过使用 Add 方法将各个 TreeNode 手动添加到集合中。

若要移除以前添加的 TreeNode,请使用 RemoveRemoveAtClear 方法。

示例

下面的代码示例将 TreeNodeCollectionTreeView 复制到临时 Array,然后使用 AddRange 方法将该数组的内容添加到另一个 TreeView 中。使用 Clear 方法将来自源 TreeViewTreeNodeCollection 清除。此示例要求您有两个 TreeView 控件,其中一个包含 TreeNode 对象的集合。

Private Sub MyButtonAddAllClick(sender As Object, e As EventArgs)
   ' Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   Dim myTreeNodeCollection As TreeNodeCollection = myTreeViewBase.Nodes
   ' Create an array of 'TreeNodes'.
   Dim myTreeNodeArray(myTreeViewBase.Nodes.Count-1) As TreeNode
   ' Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray, 0)
   ' Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear()
   ' Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom.Nodes.AddRange(myTreeNodeArray)
End Sub
private void MyButtonAddAllClick(object sender, EventArgs e)
{
   // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes;
   // Create an array of 'TreeNodes'.
   TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count];
   // Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0);
   // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear();
   // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
   myTreeViewCustom.Nodes.AddRange(myTreeNodeArray);
}
private:
   void MyButtonAddAllClick( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
      TreeNodeCollection^ myTreeNodeCollection = myTreeViewBase->Nodes;
      
      // Create an array of 'TreeNodes'.
      array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myTreeViewBase->Nodes->Count);
      
      // Copy the tree nodes to the 'myTreeNodeArray' array.
      myTreeViewBase->Nodes->CopyTo( myTreeNodeArray, 0 );
      
      // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
      myTreeViewBase->Nodes->Clear();
      
      // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom->Nodes->AddRange( myTreeNodeArray );
   }
private void myButtonAddAllClick(Object sender, EventArgs e)
{
    // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
    TreeNodeCollection myTreeNodeCollection = myTreeViewBase.get_Nodes();
    // Create an array of 'TreeNodes'.
    TreeNode myTreeNodeArray[] =
        new TreeNode[myTreeViewBase.get_Nodes().get_Count()];
    // Copy the tree nodes to the 'myTreeNodeArray' array.
    myTreeViewBase.get_Nodes().CopyTo(myTreeNodeArray, 0);
    // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
    myTreeViewBase.get_Nodes().Clear();
    // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
    myTreeViewCustom.get_Nodes().AddRange(myTreeNodeArray);
} //myButtonAddAllClick

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TreeNodeCollection 类
TreeNodeCollection 成员
System.Windows.Forms 命名空间
Add
CopyTo