TreeNode.Expand 方法

定义

展开树节点。Expands the tree node.

public:
 void Expand();
public void Expand ();
member this.Expand : unit -> unit
Public Sub Expand ()

示例

下面的代码示例在单击按钮时切换所选节点。The following code example toggles the selected node when a button is clicked. 如果所选节点处于折叠状态,则通过调用方法展开该节点,并 Expand 通过调用方法将其折叠 CollapseIf the selected node is collapsed, it is expanded, if it is expanded by calling the Expand method, it is collapsed by calling the Collapse method. 此示例要求具有一个 Form TreeView 控件,该控件至少具有 TreeNode 一个子控件 TreeNodeThis example requires that you have a Form with a TreeView control that has at least one TreeNode with at least one child TreeNode.

void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   if ( treeView1->SelectedNode->IsExpanded )
   {
      treeView1->SelectedNode->Collapse();
      MessageBox::Show( String::Concat( treeView1->SelectedNode->Text, " tree node collapsed." ) );
   }
   else
   {
      treeView1->SelectedNode->Expand();
      MessageBox::Show( String::Concat( treeView1->SelectedNode->Text, " tree node expanded." ) );
   }
}
private void button1_Click(object sender, System.EventArgs e)
{
   if (treeView1.SelectedNode.IsExpanded)
   {
      treeView1.SelectedNode.Collapse();
      MessageBox.Show(treeView1.SelectedNode.Text + 
        " tree node collapsed.");
   }
   else
   {
      treeView1.SelectedNode.Expand();
      MessageBox.Show(treeView1.SelectedNode.Text + 
        " tree node expanded.");
   }
}
Private Sub button1_Click(sender As Object, _
  e As System.EventArgs) Handles button1.Click
   If treeView1.SelectedNode.IsExpanded Then
      treeView1.SelectedNode.Collapse()
      MessageBox.Show(treeView1.SelectedNode.Text & _ 
        " tree node collapsed.")
   Else
      treeView1.SelectedNode.Expand()
      MessageBox.Show(treeView1.SelectedNode.Text & _
        " tree node expanded.")
   End If
End Sub 

注解

Expand方法将当前的向 TreeNode 下扩展到节点的下一级。The Expand method expands the current TreeNode down to the next level of nodes.

备注

的状态 TreeNode 是持久的。The state of a TreeNode is persisted. 例如,如果之前未折叠子节点的下一级别,则在 Expand 调用方法时,子节点将显示在其先前展开的状态中。For example, if the next level of child nodes was not collapsed previously, when the Expand method is called, the child nodes appear in their previously expanded state.

适用于

另请参阅