Export (0) Print
Expand All
Expand Minimize

TreeViewCancelEventHandler Delegate

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

[Visual Basic]
<Serializable>
Public Delegate Sub TreeViewCancelEventHandler( _
   ByVal sender As Object, _
   ByVal e As TreeViewCancelEventArgs _
)
[C#]
[Serializable]
public delegate void TreeViewCancelEventHandler(
   object sender,
   TreeViewCancelEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void TreeViewCancelEventHandler(
   Object* sender,
   TreeViewCancelEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the TreeViewCancelEventHandler delegate declaration.

sender
The source of the event.
e
A TreeViewCancelEventArgs that contains the event data.

Remarks

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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] This behavior can also be implemented by handling the TreeView.BeforeCollapse event, as illustrated in the example for that topic.

[Visual Basic] 
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Private treeView1 As TreeView
    Private showCheckedNodesButton As Button

    Public Sub New()
        treeView1 = New TreeView
        showCheckedNodesButton = New Button

        Me.SuspendLayout()

        ' Initialize treeView1.
        treeView1.Location = New Point(0, 25)
        treeView1.Size = New Size(292, 248)
        treeView1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
        treeView1.CheckBoxes = True

        ' Add nodes to treeView1.
        Dim node As TreeNode
        Dim x As Integer
        For x = 0 To 3
            ' Add a root node.
            node = treeView1.Nodes.Add(String.Format("Node{0}", x * 4))
            Dim y As Integer
            For y = 1 To 4
                ' Add a node as a child of the previously added node.
                node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y))
            Next y
        Next x

        ' 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 = New Size(144, 24)
        showCheckedNodesButton.Text = "Show Checked Nodes"
        AddHandler showCheckedNodesButton.Click, AddressOf showCheckedNodesButton_Click

        ' Initialize the form.
        Me.ClientSize = New Size(292, 273)
        Me.Controls.AddRange(New Control() {showCheckedNodesButton, treeView1})

        Me.ResumeLayout(False)
    End Sub 'New

    Shared Sub Main()
        Application.Run(New Form1)
    End Sub 'Main

    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

End Class 'Form1 

[C#] 
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    private TreeView treeView1;
    private Button showCheckedNodesButton;
    private TreeViewCancelEventHandler checkForCheckedChildren;

    public Form1()
    {
        treeView1 = new TreeView();
        showCheckedNodesButton = new Button();
        checkForCheckedChildren = 
            new TreeViewCancelEventHandler(CheckForCheckedChildrenHandler);

        this.SuspendLayout();

        // Initialize treeView1.
        treeView1.Location = new Point(0, 25);
        treeView1.Size = new Size(292, 248);
        treeView1.Anchor = 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 = new Size(144, 24);
        showCheckedNodesButton.Text = "Show Checked Nodes";
        showCheckedNodesButton.Click += 
            new EventHandler(showCheckedNodesButton_Click);

        // Initialize the form.
        this.ClientSize = new Size(292, 273);
        this.Controls.AddRange(new Control[] 
            { showCheckedNodesButton, treeView1 } );

        this.ResumeLayout(false);
    }

    static void Main() 
    {
        Application.Run(new Form1());
    }

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

}

[C++] 
#using <mscorlib.dll>
#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 __gc class Form1 : public Form
{
private:
    TreeView* treeView1;
    Button* showCheckedNodesButton;
    TreeViewCancelEventHandler* checkForCheckedChildren;

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

        this->SuspendLayout();

        // 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(S"Node{0}", __box(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(S"Node{0}", __box(x*4 + y)));
            }
        }

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

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

        // Initialize the form.
        this->ClientSize = System::Drawing::Size(292, 273);

        Control* temp0 [] = {showCheckedNodesButton, treeView1};
        this->Controls->AddRange(temp0 );

        this->ResumeLayout(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 = __try_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(new Form1());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

System.Windows.Forms Namespace | TreeViewCancelEventArgs | TreeView

Show:
© 2014 Microsoft