TreeViewCancelEventArgs Classe

Definizione

Fornisce i dati per gli eventi BeforeCheck, BeforeCollapse, BeforeExpand e BeforeSelect di un controllo 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
Ereditarietà
TreeViewCancelEventArgs

Esempio

Nell'esempio seguente viene illustrato come modificare lo stato di compressione di un oggetto in TreeView modo che tutti i nodi controllati siano visibili.The following example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. Prima di tutto, tutti i nodi vengono compressi e viene aggiunto un gestore all' TreeView.BeforeExpand evento.First, all the nodes are collapsed, and a handler is added to the TreeView.BeforeExpand event. Quindi, tutti i nodi vengono espansi.Next, all the nodes are expanded. Il TreeView.BeforeExpand gestore eventi determina se un determinato nodo dispone di nodi figlio che sono controllati.The TreeView.BeforeExpand event handler determines whether a given node has child nodes that are checked. Se un nodo non dispone di elementi figlio controllati, l'espansione viene annullata per tale nodo.If a node does not have checked children, the expansion is canceled for that node. Per consentire l'espansione normale del nodo quando si fa clic sul segno più accanto a un nodo, il TreeView.BeforeExpand gestore eventi viene quindi rimosso.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.

Questo comportamento può essere implementato anche gestendo l' TreeView.BeforeCollapse evento, come illustrato nell'esempio relativo a tale argomento.This behavior can also be implemented by handling the TreeView.BeforeCollapse event, as illustrated in the example for that topic.

Per l'esempio completo, vedere l' TreeView.CheckBoxes argomento di riferimento.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

Commenti

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.For more information about handling events, see Handling and Raising Events.

Costruttori

TreeViewCancelEventArgs(TreeNode, Boolean, TreeViewAction)

Inizializza una nuova istanza della classe TreeViewCancelEventArgs con il nodo della struttura ad albero specificato, un valore che specifica se l'evento deve essere annullato e il tipo di azione della visualizzazione della struttura ad albero che ha generato l'evento.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.

Proprietà

Action

Ottiene il tipo di azione TreeView che ha generato l'evento.Gets the type of TreeView action that raised the event.

Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.Gets or sets a value indicating whether the event should be canceled.

(Ereditato da CancelEventArgs)
Node

Ottiene il nodo dell'albero da controllare, espandere, comprimere o selezionare.Gets the tree node to be checked, expanded, collapsed, or selected.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Ereditato da Object)

Si applica a

Vedi anche