TreeView.BeforeExpand 事件

在展开树节点前发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event BeforeExpand As TreeViewCancelEventHandler
用法
Dim instance As TreeView
Dim handler As TreeViewCancelEventHandler

AddHandler instance.BeforeExpand, handler
public event TreeViewCancelEventHandler BeforeExpand
public:
event TreeViewCancelEventHandler^ BeforeExpand {
    void add (TreeViewCancelEventHandler^ value);
    void remove (TreeViewCancelEventHandler^ value);
}
/** @event */
public void add_BeforeExpand (TreeViewCancelEventHandler value)

/** @event */
public void remove_BeforeExpand (TreeViewCancelEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例演示如何更改 TreeView 的折叠状态,以便所有选中的节点都可见。首先,折叠所有节点,并为 BeforeExpand 事件添加一个处理程序。然后,展开所有节点。BeforeExpand 事件处理程序确定给定节点是否有选中的子节点。如果某个节点没有选中的子节点,则取消该节点的展开操作。为了在单击节点旁边的加号时允许标准的节点展开操作,BeforeExpand 事件处理程序于是被移除。

此行为还可以通过处理 BeforeCollapse 事件实现,该主题的示例中阐释了这一点。

有关完整的示例,请参见 CheckBoxes 参考主题。

Private Sub showCheckedNodesButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Disable redrawing of treeView1 to prevent flickering 
    ' while changes are made.
    treeView1.BeginUpdate()

    ' Collapse all nodes of treeView1.
    treeView1.CollapseAll()

    ' Add the CheckForCheckedChildren event handler to the BeforeExpand event.
    AddHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren

    ' Expand all nodes of treeView1. Nodes without checked children are 
    ' prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.ExpandAll()

    ' Remove the checkForCheckedChildren event handler from the BeforeExpand 
    ' event so manual node expansion will work correctly.
    RemoveHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren

    ' Enable redrawing of treeView1.
    treeView1.EndUpdate()
End Sub 'showCheckedNodesButton_Click

' Prevent expansion of a node that does not have any checked child nodes.
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
    If Not HasCheckedChildNodes(e.Node) Then
        e.Cancel = True
    End If
End Sub 'CheckForCheckedChildren

' Returns a value indicating whether the specified 
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean
    If node.Nodes.Count = 0 Then
        Return False
    End If
    Dim childNode As TreeNode
    For Each childNode In node.Nodes
        If childNode.Checked Then
            Return True
        End If
        ' Recursively check the children of the current child node.
        If HasCheckedChildNodes(childNode) Then
            Return True
        End If
    Next childNode
    Return False
End Function 'HasCheckedChildNodes
private void showCheckedNodesButton_Click(object sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering 
    // while changes are made.
    treeView1.BeginUpdate();

    // Collapse all nodes of treeView1.
    treeView1.CollapseAll();

    // Add the checkForCheckedChildren event handler to the BeforeExpand event.
    treeView1.BeforeExpand += checkForCheckedChildren;

    // Expand all nodes of treeView1. Nodes without checked children are 
    // prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.ExpandAll();

    // Remove the checkForCheckedChildren event handler from the BeforeExpand 
    // event so manual node expansion will work correctly.
    treeView1.BeforeExpand -= checkForCheckedChildren;

    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
}

// Prevent expansion of a node that does not have any checked child nodes.
private void CheckForCheckedChildrenHandler(object sender, 
    TreeViewCancelEventArgs e)
{
    if (!HasCheckedChildNodes(e.Node)) e.Cancel = true;
}

// Returns a value indicating whether the specified 
// TreeNode has checked child nodes.
private bool HasCheckedChildNodes(TreeNode node)
{
    if (node.Nodes.Count == 0) return false;
    foreach (TreeNode childNode in node.Nodes)
    {
        if (childNode.Checked) return true;
        // Recursively check the children of the current child node.
        if (HasCheckedChildNodes(childNode)) return true;
    }
    return false;
}
private:
   void showCheckedNodesButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      // Disable redrawing of treeView1 to prevent flickering 
      // while changes are made.
      treeView1->BeginUpdate();
      
      // Collapse all nodes of treeView1.
      treeView1->CollapseAll();
      
      // Add the checkForCheckedChildren event handler to the BeforeExpand event.
      treeView1->BeforeExpand += checkForCheckedChildren;
      
      // Expand all nodes of treeView1. Nodes without checked children are 
      // prevented from expanding by the checkForCheckedChildren event handler.
      treeView1->ExpandAll();
      
      // Remove the checkForCheckedChildren event handler from the BeforeExpand 
      // event so manual node expansion will work correctly.
      treeView1->BeforeExpand -= checkForCheckedChildren;
      
      // Enable redrawing of treeView1.
      treeView1->EndUpdate();
   }

   // Prevent expansion of a node that does not have any checked child nodes.
   void CheckForCheckedChildrenHandler( Object^ /*sender*/, TreeViewCancelEventArgs^ e )
   {
      if (  !HasCheckedChildNodes( e->Node ) )
            e->Cancel = true;
   }


   // Returns a value indicating whether the specified 
   // TreeNode has checked child nodes.
   bool HasCheckedChildNodes( TreeNode^ node )
   {
      if ( node->Nodes->Count == 0 )
            return false;

      System::Collections::IEnumerator^ myEnum = node->Nodes->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         TreeNode^ childNode = safe_cast<TreeNode^>(myEnum->Current);
         if ( childNode->Checked )
                  return true;

         // Recursively check the children of the current child node.
         if ( HasCheckedChildNodes( childNode ) )
                  return true;
      }

      return false;
   }
private void showCheckedNodesButton_Click(Object sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering 
    // while changes are made.
    treeView1.BeginUpdate();
    // Collapse all nodes of treeView1.
    treeView1.CollapseAll();
    // Add the checkForCheckedChildren event handler to the
    // BeforeExpand event.
    treeView1.add_BeforeExpand(checkForCheckedChildren);
    // Expand all nodes of treeView1. Nodes without checked children are 
    // prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.ExpandAll();
    // Remove the checkForCheckedChildren event handler from
    // the BeforeExpand 
    // event so manual node expansion will work correctly.
    treeView1.remove_BeforeExpand(checkForCheckedChildren);
    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
} //showCheckedNodesButton_Click

// Prevent expansion of a node that does not have any checked child nodes.
private void CheckForCheckedChildrenHandler(Object sender,
    TreeViewCancelEventArgs e)
{
    if (!(HasCheckedChildNodes(e.get_Node()))) {
        e.set_Cancel(true);
    }
} //CheckForCheckedChildrenHandler

// Returns a value indicating whether the specified 
// TreeNode has checked child nodes.
private boolean HasCheckedChildNodes(TreeNode node)
{
    if (node.get_Nodes().get_Count() == 0) {
        return false;
    }
    for (int iCtr = 0; iCtr < node.get_Nodes().get_Count(); iCtr++) {
        TreeNode childNode = node.get_Nodes().get_Item(iCtr);
        if (childNode.get_Checked()) {
            return true;
        }
        // Recursively check the children of the current child node.
        if (HasCheckedChildNodes(childNode)) {
            return true;
        }
    }
    return false;
} //HasCheckedChildNodes

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TreeView 类
TreeView 成员
System.Windows.Forms 命名空间
OnBeforeExpand
TreeView.AfterExpand 事件
OnAfterExpand