共用方式為


TreeNode.Parent 屬性

定義

取得目前樹狀節點的父樹狀節點。

public:
 property System::Windows::Forms::TreeNode ^ Parent { System::Windows::Forms::TreeNode ^ get(); };
public System.Windows.Forms.TreeNode Parent { get; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.TreeNode Parent { get; }
member this.Parent : System.Windows.Forms.TreeNode
[<System.ComponentModel.Browsable(false)>]
member this.Parent : System.Windows.Forms.TreeNode
Public ReadOnly Property Parent As TreeNode

屬性值

TreeNode

TreeNode,表示目前樹狀節點的父層。

屬性

範例

下列程式碼範例會顯示 Text 的 屬性所 Parent 表示的 TreeView.SelectedNodeIndex 屬性值 TreeNode 。 此範例會要求您具有 Form 控制項 TreeView 的 。 TreeView應該至少有兩個根節點,每個節點至少有一個子節點。

private:
   void treeView1_AfterSelect( Object^ /*sender*/, TreeViewEventArgs^ e )
   {
      /* Display the Text and Index of the
            * selected tree node's Parent. */
      if ( e->Node->Parent != nullptr && e->Node->Parent->GetType() == TreeNode::typeid )
      {
         statusBar1->Text = String::Format( "Parent: {0}\n Index Position: {1}", e->Node->Parent->Text, e->Node->Parent->Index );
      }
      else
      {
         statusBar1->Text = "No parent node.";
      }
   }
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.";
   }
}
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 (e.Node.Parent IsNot 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

備註

如果樹狀節點位於根層級,則 屬性會 Parentnull 回 。

適用於