TreeNodeCollection.Insert Method

Definition

Inserts an item in the collection at a specified location.

Overloads

Insert(Int32, String, String, String, String)

Creates a tree node with the specified key, text, and images, and inserts it into the collection at the specified index.

Insert(Int32, String, String, Int32, Int32)

Creates a tree node with the specified key, text, and images, and inserts it into the collection at the specified index.

Insert(Int32, String, String, String)

Creates a tree node with the specified key, text, and image, and inserts it into the collection at the specified index.

Insert(Int32, String, String)

Creates a tree node with the specified text and key, and inserts it into the collection.

Insert(Int32, TreeNode)

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

Insert(Int32, String)

Creates a tree node with the specified text and inserts it at the specified index.

Insert(Int32, String, String, Int32)

Creates a tree node with the specified key, text, and image, and inserts it into the collection at the specified index.

Insert(Int32, String, String, String, String)

Creates a tree node with the specified key, text, and images, and inserts it into the collection at the specified index.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ key, System::String ^ text, System::String ^ imageKey, System::String ^ selectedImageKey);
public virtual System.Windows.Forms.TreeNode Insert (int index, string key, string text, string imageKey, string selectedImageKey);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? key, string? text, string? imageKey, string? selectedImageKey);
abstract member Insert : int * string * string * string * string -> System.Windows.Forms.TreeNode
override this.Insert : int * string * string * string * string -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, key As String, text As String, imageKey As String, selectedImageKey As String) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

key
String

The name of the tree node.

text
String

The text to display in the tree node.

imageKey
String

The key of the image to display in the tree node.

selectedImageKey
String

The key of the image to display in the tree node when it is in a selected state.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

The Name property corresponds to the key for a TreeNode in the TreeNodeCollection.

The imageKey parameter refers to an image in the ImageList property of the parent TreeView.

The selectedimageKey parameter refers to an image in the StateImageList property of the parent TreeView.

Applies to

Insert(Int32, String, String, Int32, Int32)

Creates a tree node with the specified key, text, and images, and inserts it into the collection at the specified index.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ key, System::String ^ text, int imageIndex, int selectedImageIndex);
public virtual System.Windows.Forms.TreeNode Insert (int index, string key, string text, int imageIndex, int selectedImageIndex);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? key, string? text, int imageIndex, int selectedImageIndex);
abstract member Insert : int * string * string * int * int -> System.Windows.Forms.TreeNode
override this.Insert : int * string * string * int * int -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, key As String, text As String, imageIndex As Integer, selectedImageIndex As Integer) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

key
String

The name of the tree node.

text
String

The text to display in the tree node.

imageIndex
Int32

The index of the image to display in the tree node.

selectedImageIndex
Int32

The index of the image to display in the tree node when it is in a selected state.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

The Name property corresponds to the key for a TreeNode in the TreeNodeCollection.

The imageIndex parameter refers to an image in the ImageList property of the parent TreeView.

The selectedimageIndex parameter refers to an image in the StateImageList property of the parent TreeView.

Applies to

Insert(Int32, String, String, String)

Creates a tree node with the specified key, text, and image, and inserts it into the collection at the specified index.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ key, System::String ^ text, System::String ^ imageKey);
public virtual System.Windows.Forms.TreeNode Insert (int index, string key, string text, string imageKey);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? key, string? text, string? imageKey);
abstract member Insert : int * string * string * string -> System.Windows.Forms.TreeNode
override this.Insert : int * string * string * string -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, key As String, text As String, imageKey As String) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

key
String

The name of the tree node.

text
String

The text to display in the tree node.

imageKey
String

The key of the image to display in the tree node.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

The Name property corresponds to the key for a TreeNode in the TreeNodeCollection.

The imageKey parameter refers to an image in the ImageList property of the parent TreeView.

Applies to

Insert(Int32, String, String)

Creates a tree node with the specified text and key, and inserts it into the collection.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ key, System::String ^ text);
public virtual System.Windows.Forms.TreeNode Insert (int index, string key, string text);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? key, string? text);
abstract member Insert : int * string * string -> System.Windows.Forms.TreeNode
override this.Insert : int * string * string -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, key As String, text As String) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

key
String

The name of the tree node.

text
String

The text to display in the tree node.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

The Name property corresponds to the key for a TreeNode in the TreeNodeCollection.

Applies to

Insert(Int32, TreeNode)

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

public:
 virtual void Insert(int index, System::Windows::Forms::TreeNode ^ node);
public virtual void Insert (int index, System.Windows.Forms.TreeNode node);
abstract member Insert : int * System.Windows.Forms.TreeNode -> unit
override this.Insert : int * System.Windows.Forms.TreeNode -> unit
Public Overridable Sub Insert (index As Integer, node As TreeNode)

Parameters

index
Int32

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

node
TreeNode

The TreeNode to insert into the collection.

Exceptions

The node is currently assigned to another TreeView.

Examples

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.

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 != nullptr )
      {
         TreeNode^ tn = treeView1->SelectedNode;
         treeView1->Nodes->Remove( tn );
         treeView2->Nodes->Insert( treeView2->Nodes->Count, tn );
      }
   }
}
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);
      }
   }
}
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
   ' If neither TreeNodeCollection is read-only, move the 
   ' selected node from treeView1 to treeView2. 
   If Not treeView1.Nodes.IsReadOnly And Not treeView2.Nodes.IsReadOnly Then
      If (treeView1.SelectedNode IsNot Nothing) Then
         Dim tn As TreeNode = treeView1.SelectedNode
         treeView1.Nodes.Remove(tn)
         treeView2.Nodes.Insert(treeView2.Nodes.Count, tn)
      End If
   End If
End Sub

Remarks

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.

Note

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.

Applies to

Insert(Int32, String)

Creates a tree node with the specified text and inserts it at the specified index.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ text);
public virtual System.Windows.Forms.TreeNode Insert (int index, string text);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? text);
abstract member Insert : int * string -> System.Windows.Forms.TreeNode
override this.Insert : int * string -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, text As String) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

text
String

The text to display in the tree node.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

Applies to

Insert(Int32, String, String, Int32)

Creates a tree node with the specified key, text, and image, and inserts it into the collection at the specified index.

public:
 virtual System::Windows::Forms::TreeNode ^ Insert(int index, System::String ^ key, System::String ^ text, int imageIndex);
public virtual System.Windows.Forms.TreeNode Insert (int index, string key, string text, int imageIndex);
public virtual System.Windows.Forms.TreeNode Insert (int index, string? key, string? text, int imageIndex);
abstract member Insert : int * string * string * int -> System.Windows.Forms.TreeNode
override this.Insert : int * string * string * int -> System.Windows.Forms.TreeNode
Public Overridable Function Insert (index As Integer, key As String, text As String, imageIndex As Integer) As TreeNode

Parameters

index
Int32

The location within the collection to insert the node.

key
String

The name of the tree node.

text
String

The text to display in the tree node.

imageIndex
Int32

The index of the image to display in the tree node.

Returns

The TreeNode that was inserted in the collection.

Remarks

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

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

The Name property corresponds to the key for a TreeNode in the TreeNodeCollection.

The imageIndex parameter refers to an image in the ImageList property of the parent TreeView.

Applies to