TreeNode.Nodes Property

Gets the collection of TreeNode objects assigned to the current tree node.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public TreeNodeCollection Nodes { get; }
/** @property */
public TreeNodeCollection get_Nodes ()

public function get Nodes () : TreeNodeCollection

Not applicable.

Property Value

A TreeNodeCollection that represents the tree nodes assigned to the current tree node.

The Nodes property can hold a collection of other TreeNode objects. Each of the tree node in the collection has a Nodes property that can contain its own TreeNodeCollection. This nesting of tree nodes can make it difficult to navigate a tree structure. The FullPath property makes it easier to determine your location in a tree.

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 TreeView.SelectedNode 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 two TreeView controls named treeView1 and treeView2, and a Button on a Form.

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 void button1_Click(Object sender, EventArgs e)
{
    // If neither TreeNodeCollection is read-only, move the 
    // selected node from treeView1 to treeView2.
    if (!(treeView1.get_Nodes().get_IsReadOnly())
        && !(treeView2.get_Nodes().get_IsReadOnly())) {
        if (treeView1.get_SelectedNode() != null) {
            TreeNode tn = treeView1.get_SelectedNode();
            treeView1.get_Nodes().Remove(tn);
            treeView2.get_Nodes().Insert(treeView2.get_Nodes().
                get_Count(), tn);
        }
    }
} //button1_Click

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.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: