cascading treeview checkbox changes
-
In a Windows form, I have a treeview/listview pair in a file backup app that behaves like Windows Explorer, with checkboxes for each element to keep track of what's been selected. I'm looking for the best way to cascade a treeview check/uncheck from a parent node to all children nodes... for example, if I check "c:\winnt", I want any child nodes to also be checked, to show that everything underneath is also selected. I'm wondering if .NET provides any built-in way to do this, or if I have to come up with my own algorithm to traverse the nodes myself... seems like a common operation in treeviews... thanks for any help.
-
In a Windows form, I have a treeview/listview pair in a file backup app that behaves like Windows Explorer, with checkboxes for each element to keep track of what's been selected. I'm looking for the best way to cascade a treeview check/uncheck from a parent node to all children nodes... for example, if I check "c:\winnt", I want any child nodes to also be checked, to show that everything underneath is also selected. I'm wondering if .NET provides any built-in way to do this, or if I have to come up with my own algorithm to traverse the nodes myself... seems like a common operation in treeviews... thanks for any help.
vlusardi, There is no built in way to do this, however a simple function like the following should do what you want:
private void CheckChildNodes(TreeNode ParentNode) { foreach(TreeNode t in ParentNode.Nodes) { t.Checked = ParentNode.Checked; if(t.Nodes.Count > 0) { this.CheckChildNodes(t); } } }
-
vlusardi, There is no built in way to do this, however a simple function like the following should do what you want:
private void CheckChildNodes(TreeNode ParentNode) { foreach(TreeNode t in ParentNode.Nodes) { t.Checked = ParentNode.Checked; if(t.Nodes.Count > 0) { this.CheckChildNodes(t); } } }