Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. cascading treeview checkbox changes

cascading treeview checkbox changes

Scheduled Pinned Locked Moved C#
csharpalgorithmshelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vlusardi
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • V vlusardi

      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.

      D Offline
      D Offline
      Don_s
      wrote on last edited by
      #2

      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);
      		}
      	}
      }
      
      V 1 Reply Last reply
      0
      • D Don_s

        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);
        		}
        	}
        }
        
        V Offline
        V Offline
        vlusardi
        wrote on last edited by
        #3

        Don, Thanks again, I knew I'd be doing some recursion if I couldn't find any built-in functionality, just didn't realize it would be that simple...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups