TreeNode.Toggle 方法

定义

将树节点切换为展开或折叠状态。

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

示例

下面的代码示例删除 TreeNode 用户右键单击鼠标上方的鼠标,并在用户单击鼠标滚轮时将其从展开切换为折叠。 此示例要求你具有FormTreeView控件。 应 TreeView 具有两个或多个根树节点,每个节点至少有一个子节点。

private:
   void treeView1_MouseDown( Object^ /*sender*/, MouseEventArgs^ e )
   {
      switch ( e->Button )
      {
         // Remove the TreeNode under the mouse cursor
         // if the right mouse button was clicked.
         case ::MouseButtons::Right:
            treeView1->GetNodeAt( e->X, e->Y )->Remove();
            break;

         // Toggle the TreeNode under the mouse cursor
         // if the middle mouse button (mouse wheel) was clicked.
         case ::MouseButtons::Middle:
            treeView1->GetNodeAt( e->X, e->Y )->Toggle();
            break;
      }
   }
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
   switch(e.Button)
   {
      // Remove the TreeNode under the mouse cursor 
      // if the right mouse button was clicked. 
      case MouseButtons.Right:
         treeView1.GetNodeAt(e.X, e.Y).Remove();
         break;
      
      // Toggle the TreeNode under the mouse cursor 
      // if the middle mouse button (mouse wheel) was clicked. 
      case MouseButtons.Middle:
         treeView1.GetNodeAt(e.X, e.Y).Toggle();
         break;
   }
}
Private Sub treeView1_MouseDown(sender As Object, _
  e As MouseEventArgs) Handles treeView1.MouseDown
   Select Case e.Button
      ' Remove the TreeNode under the mouse cursor 
      ' if the right mouse button was clicked. 
      Case MouseButtons.Right
         treeView1.GetNodeAt(e.X, e.Y).Remove()
      
      ' Toggle the TreeNode under the mouse cursor 
      ' if the middle mouse button (mouse wheel) was clicked. 
      Case MouseButtons.Middle
         treeView1.GetNodeAt(e.X, e.Y).Toggle()
   End Select
End Sub

注解

将树节点切换为与其当前状态相反的状态(展开或折叠)。

备注

持久化状态 TreeNode 。 例如,如果以前未折叠下一级别的子节点,则调用该方法时 Expand ,子节点会显示在其以前展开的状态。

适用于

另请参阅