This documentation is archived and is not being maintained.

TreeViewCancelEventHandler Delegate

Represents the method that will handle the BeforeCheck, BeforeCollapse, BeforeExpand, or BeforeSelect event of a TreeView.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public delegate void TreeViewCancelEventHandler(
	Object^ sender, 
	TreeViewCancelEventArgs^ e


Type: System::Object
The source of the event.
Type: System.Windows.Forms::TreeViewCancelEventArgs
A TreeViewCancelEventArgs that contains the event data.

When you create a TreeViewCancelEventArgs delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

The following example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. First, all the nodes are collapsed, and a handler is added to the TreeView::BeforeExpand event. Next, all the nodes are expanded. The TreeView::BeforeExpand event handler determines whether a given node has child nodes that are checked. If a node does not have checked children, the expansion is canceled for that node. 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.

This behavior can also be implemented by handling the TreeView::BeforeCollapse event, as illustrated in the example for that topic.

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public Form
   TreeView^ treeView1;
   Button^ showCheckedNodesButton;
   TreeViewCancelEventHandler^ checkForCheckedChildren;

      treeView1 = gcnew TreeView;
      showCheckedNodesButton = gcnew Button;
      checkForCheckedChildren = gcnew TreeViewCancelEventHandler( this, &Form1::CheckForCheckedChildrenHandler );

      // Initialize treeView1.
      treeView1->Location = Point(0,25);
      treeView1->Size = System::Drawing::Size( 292, 248 );
      treeView1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top | AnchorStyles::Left | AnchorStyles::Bottom | AnchorStyles::Right);
      treeView1->CheckBoxes = true;

      // Add nodes to treeView1.
      TreeNode^ node;
      for ( int x = 0; x < 3; ++x )

         // Add a root node.
         node = treeView1->Nodes->Add( String::Format( "Node{0}", x * 4 ) );
         for ( int y = 1; y < 4; ++y )

            // Add a node as a child of the previously added node.
            node = node->Nodes->Add( String::Format( "Node{0}", x * 4 + y ) );


      // Set the checked state of one of the nodes to
      // demonstrate the showCheckedNodesButton button behavior.
      treeView1->Nodes[ 1 ]->Nodes[ 0 ]->Nodes[ 0 ]->Checked = true;

      // Initialize showCheckedNodesButton.
      showCheckedNodesButton->Size = System::Drawing::Size( 144, 24 );
      showCheckedNodesButton->Text = "Show Checked Nodes";
      showCheckedNodesButton->Click += gcnew EventHandler( this, &Form1::showCheckedNodesButton_Click );

      // Initialize the form.
      this->ClientSize = System::Drawing::Size( 292, 273 );
      array<Control^>^temp0 = {showCheckedNodesButton,treeView1};
      this->Controls->AddRange( temp0 );
      this->ResumeLayout( false );

   void showCheckedNodesButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
      // Disable redrawing of treeView1 to prevent flickering 
      // while changes are made.

      // Collapse all nodes of treeView1.

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

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

      // Enable redrawing of treeView1.

   // 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;

int main()
   Application::Run( gcnew Form1 );

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.