This documentation is archived and is not being maintained.

TreeNodeCollection.AddRange Method

Adds an array of previously created tree nodes to the collection.

[Visual Basic]
Public Overridable Sub AddRange( _
   ByVal nodes() As TreeNode _
)
[C#]
public virtual void AddRange(
 TreeNode[] nodes
);
[C++]
public: virtual void AddRange(
 TreeNode* nodes[]
);
[JScript]
public function AddRange(
   nodes : TreeNode[]
);

Parameters

nodes
An array of TreeNode objects representing the tree nodes to add to the collection.

Remarks

The TreeNode objects contained in the nodes array are appended to the end of the collection.

You can use the AddRange method to quickly add a group of previously created TreeNode objects to the collection instead of manually adding each TreeNode to the collection using the Add method.

To remove a TreeNode that you previously added, use the Remove, RemoveAt or Clear methods.

Example

[Visual Basic, C#, C++] The following example copies the TreeNodeCollection from a TreeView to a temporary Array, then adds the contents of the array to another TreeView using the AddRange method. The TreeNodeCollection from the source TreeView is cleared using the Clear method. This example assumes you have two TreeView controls, one with a collection of TreeNode objects.

[Visual Basic] 
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

[C#] 
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);
}

[C++] 
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);
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace | Add | CopyTo

Show: