Display Three State Checkboxes in TreeView .NET Control

Created: Mar 05, 2009

Updated: Jun 01, 2013

By default the IntegralUI TreeView displays two states of the checkboxes in each node. By setting the CheckMode property to ThreeState, every check box can display one of these three states: Unchecked, Indeterminate and Checked.

However, the check box value is not automatically updated for any child or parent node. To do that, the AfterCheck event needs to be handled. Here is how:

At first you need to have a TreeView with some root nodes and child nodes in it. Also nodes must have check box visible and the tri-state mode enabled. In the following code we will create a small tree hierarchy:

Then by handling the AfterCheck event, we will cycle through child and parent nodes and change the value of their check box. The UpdateChild method is used to change the check box value of the child nodes, while UpdateParent method is used to change the check box value of the parent nodes.

In order to avoid callback internally whenever a value of check box s changed when AfterCheck event is called, we will use a variable which determines whether the event is already called or not.

For Each childNode As LidorSystems.IntegralUI.Lists.TreeNode In parentNode.Nodes

If childNode.Visible Then

numVisible += 1

' If there is at least one Indeterminate node, exit the loop.

' The state for the parent node will be also Indeterminate

If childNode.CheckState = CheckState.Indeterminate Then

state = CheckState.Indeterminate

Exit For

' Count the unchecked nodes

ElseIf childNode.CheckState = CheckState.Unchecked Then

numUnchecked += 1

End If

End If

Next

If numVisible > 0 Then

' If there are no unchecked nodes and there is at least one indeterminate node,

' that means that all child nodes are checked. So, the parent node will also be Checked

If numUnchecked = 0 AndAlso state <> CheckState.Indeterminate Then

state = CheckState.Checked

' If number of visible and unchecked nodes are equal, then parent node will be Unchecked

ElseIf numUnchecked = numVisible Then

state = CheckState.Unchecked

Else

' In any other case, then parent node will be Indeterminate

state = CheckState.Indeterminate

End If

Else

' If there are no visible nodes, the parent node will be unchecked

state = CheckState.Unchecked

End If

' Use this method to change the CheckState of the node,

' without triggerring the BeforeCheck and AfterCheck events

' this.treeView1.ChangeCheckState(parentNode, state);

parentNode.CheckState = state

' Repeat the whole cycle for other parent nodes

parentNode = parentNode.Parent

End While

End Sub

A complete sample project in C# .NET and VB .NET, showing how to create nodes with tri-state checkboxes in TreeView, is available for download from here: TreeView .NET - Three State Checkboxes

Tab content switching

Newsletter

Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.

Name:

Email: *

*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.