This documentation is archived and is not being maintained.

TreeNode.Parent Property

Gets the parent tree node of the current tree node.

[Visual Basic]
Public ReadOnly Property Parent As TreeNode
[C#]
public TreeNode Parent {get;}
[C++]
public: __property TreeNode* get_Parent();
[JScript]
public function get Parent() : TreeNode;

Property Value

A TreeNode that represents the parent of the current tree node.

Remarks

If the tree node is at the root level, the Parent property returns a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following example displays the Text and Index property values of the TreeNode represented by the Parent property of the TreeView.SelectedNode. This example assumes you have a Form with a TreeView control on it. The TreeView should have at least two root nodes, each having at least one child node.

[Visual Basic] 
Private Sub treeView1_AfterSelect(sender As Object, _
  e As TreeViewEventArgs) Handles treeView1.AfterSelect
   ' Display the Text and Index of the 
   ' selected tree node's Parent. 
   If (Not e.Node.Parent Is Nothing) 
      If (e.Node.Parent.GetType() Is GetType(TreeNode)) Then
         statusBar1.Text = "Parent: " + e.Node.Parent.Text + _
           ControlChars.Cr + "Index Position: " + e.Node.Parent.Index.ToString()
      End If
   Else
      statusBar1.Text = "No parent node."
   End If
End Sub 

[C#] 
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{  
   /* Display the Text and Index of the 
    * selected tree node's Parent. */
   if(e.Node.Parent!= null && 
     e.Node.Parent.GetType() == typeof(TreeNode) )
   {
      statusBar1.Text = "Parent: " + e.Node.Parent.Text + "\n"
         + "Index Position: " + e.Node.Parent.Index.ToString();
   }
   else
   {
      statusBar1.Text = "No parent node.";
   }
}

[C++] 
private:
   void treeView1_AfterSelect(Object* /*sender*/, TreeViewEventArgs* e) {
      /* Display the Text and Index of the
      * selected tree node's Parent. */
      if (e->Node->Parent!= 0 &&
         e->Node->Parent->GetType() == __typeof(TreeNode)) {
            statusBar1->Text = String::Format( S"Parent: {0}\n Index Position: {1}", e->Node->Parent->Text, __box(e->Node->Parent->Index));
         } else {
            statusBar1->Text = S"No parent node.";
         }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace

Show: