This documentation is archived and is not being maintained.

TreeView.CheckBoxes Property

Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control.

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

public bool CheckBoxes { get; set; }

Property Value

Type: System.Boolean
true if a check box is displayed next to each tree node in the tree view control; otherwise, false. The default is false.

A check box is displayed to the left of both the tree node label and tree node Image, if any. Check boxes allow the user to select more than one tree node at a time.

When the CheckBoxes property of a TreeView is set to true, and the StateImageList property is set, each TreeNode contained in the TreeView will display the first and second images from the StateImageList to indicate an unchecked or checked state, respectively. However, the displayed images may be different if the following situation occurs: If the SelectedImageIndex of the node is set to a value other than 0 or 1, and the CheckBoxes property of the parent TreeView is set to false, the SelectedImageIndex will not automatically reset to -1 to indicate it is not set. In this case, the state image at the indicated index position is shown. If CheckBoxes is set to true again, the first and second images of the StateImageList are shown to indicate a checked or unchecked state, respectively.


When setting the CheckBoxes property at run time, the TreeView handle is recreated (see Control.RecreateHandle) to update the control's appearance. This causes all tree nodes to be collapsed, with the exception of the selected TreeNode.

The following code 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 for the BeforeExpand event. Next, all the nodes are expanded. The 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 standard node expansion when the plus sign next to a node is clicked, the BeforeExpand event handler is then removed.

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

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


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


    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.

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


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0