Freigeben über


TreeView.BeforeExpand-Ereignis

Tritt ein, bevor der Strukturknoten erweitert wird.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event BeforeExpand As TreeViewCancelEventHandler
'Usage
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 unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird das Ändern des Zustands einer TreeView veranschaulicht, um alle aktivierten Knoten anzuzeigen. Zu Beginn sind alle Knoten reduziert, und für das BeforeExpand-Ereignis wird ein Handler hinzugefügt. Anschließend werden alle Knoten erweitert. Der BeforeExpand-Ereignishandler bestimmt, ob ein bestimmter Knoten über aktivierte untergeordnete Knoten verfügt. Wenn ein Knoten über keine aktivierten untergeordneten Knoten verfügt, wird die Erweiterung an dem Knoten beendet. Damit Knoten wieder standardmäßig über Klicken auf das Pluszeichen neben dem Knoten erweitert werden können, wird der BeforeExpand-Ereignishandler anschließend entfernt.

Dieses Verhalten können Sie auch durch Behandeln des BeforeCollapse-Ereignisses implementieren. Das entsprechende Vorgehen wird im Beispiel zu dem Thema veranschaulicht.

Das vollständige Beispiel finden Sie unter dem Referenzthema zu 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

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

TreeView-Klasse
TreeView-Member
System.Windows.Forms-Namespace
OnBeforeExpand
TreeView.AfterExpand-Ereignis
OnAfterExpand