Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Clear Method

TreeNodeCollection.Clear Method

Removes all tree nodes from the collection.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual void Clear()

Implements

IList.Clear()

You can use this method to clear the entire collection of tree nodes from a tree view.

To remove an individual tree node from the collection, use the Remove or RemoveAt methods.

To add new TreeNode objects to the collection, use the Add, AddRange, or Insert 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);
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft