Export (0) Print
Expand All

TreeNodeCollection.Insert Method (Int32, TreeNode)

Inserts an existing tree node into the tree node collection at the specified location.

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

public virtual void Insert(
	int index,
	TreeNode node
)

Parameters

index
Type: System.Int32

The indexed location within the collection to insert the tree node.

node
Type: System.Windows.Forms.TreeNode

The TreeNode to insert into the collection.

ExceptionCondition
ArgumentException

The node is currently assigned to another TreeView.

If the TreeView.Sorted property is set to true, the index parameter value is ignored. The TreeNode is inserted into the tree view, and the TreeView resorted.

You can also add new TreeNode objects to the collection by using the Add or AddRange methods.

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

NoteNote

A TreeNode can be assigned to only one TreeView control at a time. To add the tree node to a new tree view control, you must remove it from the other tree view first or clone it.

The following code example removes the selected tree node from one TreeView and adds it to another if both tree node collections are not read-only. When a Button is clicked, the TreeNode represented by the TreeView.SelectedNode property is deleted from one TreeView using the Remove method and added to the other TreeView using the Insert method. This example requires that you have a Form that contains two TreeView controls and a Button. The TreeView controls should be named treeView1 and treeView2.

private void button1_Click(object sender, EventArgs e)
{
   // If neither TreeNodeCollection is read-only, move the  
   // selected node from treeView1 to treeView2. 
   if(!treeView1.Nodes.IsReadOnly && !treeView2.Nodes.IsReadOnly)
   {
      if(treeView1.SelectedNode != null)
      {
         TreeNode tn = treeView1.SelectedNode;
         treeView1.Nodes.Remove(tn);
         treeView2.Nodes.Insert(treeView2.Nodes.Count, tn);
      }
   }
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft