HI all,
I have develop a windows application using VB.net2.0, in that I have a UltraTree control to display the details in 3 level (Master,child and GrandChild) with checkbox.
when i click the master tree checkbox then automatically all child and GrandChild checkbox are marked and vice versa.If any one child or grandchild as unmarked means then the corresponding parent are unmard. which event i call, i need you socution with coding.
Advance thanks for your reply
@maguesh
I'd like to share the implementation I came up with, based on the Knowledge Base article. Feel free to use as you wish and to improve it. :-)
The checkbox/selection tree view behaves the way I expect it to:
If a node's CheckState changes, then all child nodes (recursively) are updated to the same state (Checked or Unchecked), and also the CheckState of the parent nodes (recursively) are recalculated (to Checked, Unchecked or Indeterminate).
(Of course just put this in a class that inherits UltraTree and you're set for reusal.)
---
Imports Infragistics.Win.UltraWinTree Public Class Form1 Inherits System.Windows.Forms.Form ''' <summary> ''' The updatingLock is used because we only want to take ''' special actions (HandleTreeCheckedStateChanges(...)) ''' on Check changes caused by user actions (e.g. clicks) ''' and ignore programmatic CheckState changes. ''' </summary> Private updatingLock As Boolean = False #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents baseTree As Infragistics.Win.UltraWinTree.UltraTree <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.baseTree = New Infragistics.Win.UltraWinTree.UltraTree() CType(Me.baseTree, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'ultraTree1 ' Me.baseTree.Dock = System.Windows.Forms.DockStyle.Fill Me.baseTree.Location = New System.Drawing.Point(0, 0) Me.baseTree.Name = "baseTree" Me.baseTree.Size = New System.Drawing.Size(292, 266) Me.baseTree.TabIndex = 1 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.baseTree) Me.Name = "Form1" Me.Text = "Form1" CType(Me.baseTree, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CreateExampleTree() ' Add checkboxes to tree Me.baseTree.Override.NodeStyle = NodeStyle.CheckBoxTriState End Sub Private Sub CreateExampleTree() Dim i, j, k As Integer For i = 1 To 5 Dim rootNode As UltraTreeNode = Me.baseTree.Nodes.Add("Node " + i.ToString()) For j = 1 To 5 Dim childNode As UltraTreeNode childNode = rootNode.Nodes.Add(rootNode.Key + " Child " + j.ToString()) For k = 1 To 5 Dim childchildNode As UltraTreeNode childchildNode = childNode.Nodes.Add(childNode.Key + " Child " + k.ToString()) Next k Next j Next i End Sub Private Sub baseTree_BeforeCheck(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinTree.BeforeCheckEventArgs) Handles baseTree.BeforeCheck Dim treeNode As UltraTreeNode = e.TreeNode ' Do not allow Indeterminate state for leaves. If IsLeafNode(treeNode) Then treeNode.Override.NodeStyle = NodeStyle.CheckBox End Sub Private Sub baseTree_AfterCheck(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinTree.NodeEventArgs) Handles baseTree.AfterCheck If updatingLock Then Return 'Because the Check change was caused programatically in HandleTreeCheckedStateChanges(...) Dim treeNode As UltraTreeNode = e.TreeNode updatingLock = True 'Begin handling checked state changes of the tree HandleTreeCheckedStateChanges(TreeNode) updatingLock = False 'End handling checked state changes of the tree End Sub ''' <summary> ''' When a tree node has been clicked and its CheckState has ''' changed we need to update the CheckState of the nodes ''' below and above in the tree. ''' </summary> Private Sub HandleTreeCheckedStateChanges(ByVal treeNode As UltraTreeNode) ' Each time a non-leaf node's CheckState changes ' 1) Make sure it is Checked or Unchecked - we should not be able to make it indeterminate by clicking! If treeNode.CheckedState = CheckState.Indeterminate Then treeNode.CheckedState = CheckState.Unchecked ' Since NodeStates: Unchecked => Checked => Indeterminate ' 2) Set all child nodes to same state. SetChildNodesCheckStatesToSameAs(treeNode) UpdateParentNodeCheckState(treeNode) End Sub Private Sub SetChildNodesCheckStatesToSameAs(ByVal treeNode As UltraTreeNode) If IsLeafNode(treeNode) Then Return 'End recursion For Each childNode As UltraTreeNode In treeNode.Nodes childNode.CheckedState = treeNode.CheckedState SetChildNodesCheckStatesToSameAs(childNode) Next End Sub ''' <summary> ''' Verifies the CheckState of the parent node for the ''' specified treeNode, by setting the parent node's ''' CheckState property based on whether none, some, ''' or all of its child nodes are currently checked. ''' </summary> ''' <param name="treeNode"></param> ''' <remarks>This method only has relevance for non root-level nodes</remarks> Private Sub UpdateParentNodeCheckState(ByVal treeNode As UltraTreeNode) Dim parentNode As UltraTreeNode = treeNode.Parent If parentNode Is Nothing Then Return 'End recursion ' Get the nodes collection to which the specified childNode belongs Dim nodesCollection As TreeNodesCollection = treeNode.ParentNodesCollection ' count of the number of checked nodes Dim checkedNodesCount As Integer = 0 Dim indeterminateCount As Integer = 0 For Each node As UltraTreeNode In nodesCollection If node.CheckedState = CheckState.Checked Then checkedNodesCount += 1 If node.CheckedState = CheckState.Indeterminate Then indeterminateCount += 1 Next ' Set appropriate CheckState of the parentNode If checkedNodesCount = nodesCollection.Count Then parentNode.CheckedState = CheckState.Checked ElseIf checkedNodesCount = 0 AndAlso indeterminateCount = 0 Then parentNode.CheckedState = CheckState.Unchecked Else parentNode.CheckedState = CheckState.Indeterminate End If ' Traverse up the tree... UpdateParentNodeCheckState(parentNode) End Sub Private Function IsLeafNode(ByVal treeNode As UltraTreeNode) As Boolean Return treeNode.Nodes.Count = 0 End Function End Class
Hey Ulf Akerstad
Thanks for posting up a great sollution and sharing it with us fellow devs !
Here is the the same in C#.
public partial class Form1 : Form { bool updatingLock = false; public Form1() { InitializeComponent(); //UIElementDrawParams.CheckBoxGlyphInfo = UIElementDrawParams.Office2007CheckBoxGlyphInfo; } private void uTreeSample_BeforeCheck(object sender, BeforeCheckEventArgs e) { UltraTreeNode treeNode = e.TreeNode; // Do not allow Indeterminate state for leaves. if (IsLeafNode(treeNode)) treeNode.Override.NodeStyle = NodeStyle.CheckBox; } private void CreateExampleTree() { int i = 0; int j = 0; int k = 0; for (i = 1; i <= 5; i++) { UltraTreeNode rootNode = this.uTreeSample.Nodes.Add("Node " + i.ToString()); for (j = 1; j <= 5; j++) { UltraTreeNode childNode = default(UltraTreeNode); childNode = rootNode.Nodes.Add(rootNode.Key + " Child " + j.ToString()); for (k = 1; k <= 5; k++) { UltraTreeNode childchildNode = default(UltraTreeNode); childchildNode = childNode.Nodes.Add(childNode.Key + " Child " + k.ToString()); } } } } private void uTreeSample_AfterCheck(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e) { if (updatingLock) return; //Because the Check change was caused programatically in HandleTreeCheckedStateChanges(...) UltraTreeNode treeNode = e.TreeNode; updatingLock = true; //Begin handling checked state changes of the tree HandleTreeCheckedStateChanges(treeNode); //End handling checked state changes of the tree updatingLock = false; } private void HandleTreeCheckedStateChanges(UltraTreeNode treeNode) { // Each time a non-leaf node's CheckState changes // 1) Make sure it is Checked or Unchecked - we should not be able to make it indeterminate by clicking! if (treeNode.CheckedState == CheckState.Indeterminate) treeNode.CheckedState = CheckState.Unchecked; // Since NodeStates: Unchecked => Checked => Indeterminate // 2) Set all child nodes to same state. SetChildNodesCheckStatesToSameAs(treeNode); UpdateParentNodeCheckState(treeNode); } private void SetChildNodesCheckStatesToSameAs(UltraTreeNode treeNode) { if (IsLeafNode(treeNode)) return; //End recursion foreach (UltraTreeNode childNode in treeNode.Nodes) { childNode.CheckedState = treeNode.CheckedState; SetChildNodesCheckStatesToSameAs(childNode); } } private void UpdateParentNodeCheckState(UltraTreeNode treeNode) { UltraTreeNode parentNode = treeNode.Parent; if (parentNode == null) return; //End recursion // Get the nodes collection to which the specified childNode belongs TreeNodesCollection nodesCollection = treeNode.ParentNodesCollection; // count of the number of checked nodes int checkedNodesCount = 0; int indeterminateCount = 0; foreach (UltraTreeNode node in nodesCollection) { if (node.CheckedState == CheckState.Checked) checkedNodesCount += 1; if (node.CheckedState == CheckState.Indeterminate) indeterminateCount += 1; } // Set appropriate CheckState of the parentNode if (checkedNodesCount == nodesCollection.Count) { parentNode.CheckedState = CheckState.Checked; } else if (checkedNodesCount == 0 && indeterminateCount == 0) { parentNode.CheckedState = CheckState.Unchecked; } else { parentNode.CheckedState = CheckState.Indeterminate; } // Traverse up the tree... UpdateParentNodeCheckState(parentNode); } private bool IsLeafNode(UltraTreeNode treeNode) { return treeNode.Nodes.Count == 0; } private void Form1_Load(object sender, EventArgs e) { CreateExampleTree(); } private void ultraButton1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); }
Fabulous! Great code.
This should be part of the standard functionality of the UltraTree.
Brilliant
Helped me solve same issue been struggling with for hours.
Thanks