TreeNodeCollection.AddRange Method
Assembly: System.Windows.Forms (in system.windows.forms.dll)
| Exception type | Condition |
|---|---|
| nodes is a null reference (Nothing in Visual Basic). | |
| nodes is the child of another TreeView. |
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.
The following code example copies the TreeNodeCollection from a TreeView to a temporary Array, and 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 requires that you have two TreeView controls, one with a collection of TreeNode objects.
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.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 Server 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
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.