TreeNodeCollection.Clear Method
.NET Framework 4
Removes all tree nodes from the collection.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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); }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.