TreeViewCancelEventArgs Classe

Définition

Fournit des données pour les événements BeforeCheck, BeforeCollapse, BeforeExpand et BeforeSelect d'un contrôle TreeView.Provides data for the BeforeCheck, BeforeCollapse, BeforeExpand, and BeforeSelect events of a TreeView control.

public ref class TreeViewCancelEventArgs : System::ComponentModel::CancelEventArgs
public class TreeViewCancelEventArgs : System.ComponentModel.CancelEventArgs
type TreeViewCancelEventArgs = class
    inherit CancelEventArgs
Public Class TreeViewCancelEventArgs
Inherits CancelEventArgs
Héritage
TreeViewCancelEventArgs

Exemples

L’exemple suivant montre comment modifier l’état de réduction d’un TreeView de sorte que tous les nœuds activés soient visibles.The following example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. Tout d’abord, tous les nœuds sont réduits et un gestionnaire est ajouté TreeView.BeforeExpand à l’événement.First, all the nodes are collapsed, and a handler is added to the TreeView.BeforeExpand event. Ensuite, tous les nœuds sont développés.Next, all the nodes are expanded. Le TreeView.BeforeExpand gestionnaire d’événements détermine si un nœud donné a des nœuds enfants qui sont vérifiés.The TreeView.BeforeExpand event handler determines whether a given node has child nodes that are checked. Si un nœud n’a pas d’enfants activés, l’expansion est annulée pour ce nœud.If a node does not have checked children, the expansion is canceled for that node. Pour permettre l’extension de nœud normale quand un utilisateur clique sur le signe plus en regard d’un TreeView.BeforeExpand nœud, le gestionnaire d’événements est alors supprimé.In order to allow normal node expansion when the plus sign next to a node is clicked, the TreeView.BeforeExpand event handler is then removed.

Ce comportement peut également être implémenté en gérant l' TreeView.BeforeCollapse événement, comme illustré dans l’exemple de cette rubrique.This behavior can also be implemented by handling the TreeView.BeforeCollapse event, as illustrated in the example for that topic.

Pour obtenir un exemple complet, consultez TreeView.CheckBoxes la rubrique de référence.For the complete example, see the TreeView.CheckBoxes reference topic.

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.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 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

' 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

' 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

Remarques

Pour plus d'informations sur la gestion des événements, voir gestion et déclenchement d’événements.For more information about handling events, see Handling and Raising Events.

Constructeurs

TreeViewCancelEventArgs(TreeNode, Boolean, TreeViewAction)

Initialise une nouvelle instance de la classe TreeViewCancelEventArgs avec le nœud d'arbre spécifié, une valeur spécifiant si l'événement doit être annulé, et le type d'action sur l'arborescence qui a déclenché l'événement.Initializes a new instance of the TreeViewCancelEventArgs class with the specified tree node, a value specifying whether the event is to be canceled, and the type of tree view action that raised the event.

Propriétés

Action

Obtient le type d'action TreeView qui a déclenché l'événement.Gets the type of TreeView action that raised the event.

Cancel

Obtient ou définit une valeur indiquant si l'événement doit être annulé.Gets or sets a value indicating whether the event should be canceled.

(Hérité de CancelEventArgs)
Node

Obtient le nœud d’arbre à activer, développer, réduire ou sélectionner.Gets the tree node to be checked, expanded, collapsed, or selected.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.Determines whether the specified object is equal to the current object.

(Hérité de Object)
GetHashCode()

Sert de fonction de hachage par défaut.Serves as the default hash function.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.Gets the Type of the current instance.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.Creates a shallow copy of the current Object.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actif.Returns a string that represents the current object.

(Hérité de Object)

S’applique à

Voir aussi