WinForms TreeView With Checked Nodes Recursion

2019-08-17 17:59发布

Im struggling with what I think should be a basic recursion of my Treeview but I cant seem to get the logic correct.

Essentially I have a WinForms Treeview (C# Net Framework 4.5.1) which has a list of nodes which are standard Nodes (no custom controls) which have checkboxes enabled for each node.

It is a dynamic Treeview so the nodes are not a fixed length deep, but can essentially has any number of SubNodes which can also have more Subnodes (hope that makes sense).

What I am trying to achieve is returning the Top Level Checked Nodes if all there Child Nodes are all checked or if not all child nodes are checked, return the list of Checked Nodes.

Some Examples are below :-

  • Node 1 (X)
    • Node 2 (X)
      • Node 3 (X)
      • Node 4 (X)
      • Node 5 (X)

This example would return Node 1 as all its Sub Nodes (and subsequent subnodes) are checked

  • Node 1 (X)
    • Node 2 (X)
      • Node 3 (X)
      • Node 4
      • Node 5 (X)

This would return Node 1 / Node 2 / Node 3 / Node 5

  • Node 1 (X)
    • Node 2 (X)
      • Node 3
      • Node 4
      • Node 5 (X)
        • Node 6 (X)
        • Node 7 (X)

This would return Node 1 / Node 2 / Node 5

  • Node 1 (X)
    • Node 2 (X)
      • Node 3
      • Node 4 (X)
      • Node 5 (X)
        • Node 6
        • Node 7 (X)

This would return Node 1 / Node 2 / Node 4 / Node 5 / Node 7

Any help would be appreciated.

Thanks.

1条回答
淡お忘
2楼-- · 2019-08-17 18:23

Hi Deacon Kelly, you can use this method.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestingApplication
{
    public partial class frmTreeView : Form
    {
        TreeView tr = new TreeView();
        TreeNode tn = new TreeNode();
        TreeNode tn1 = new TreeNode();

        public frmTreeView()
        {
            InitializeComponent();
        }

        private void frmTreeView_Load(object sender, EventArgs e)
        {
            tr.CheckBoxes = true;
            tn.Text = "fruit";
            tn1.Text = "snack";
            tn1.Nodes.Add("meat");
            tn1.Nodes.Add("other");
            tn.Nodes.Add("apple");
            tn.Nodes.Add("orange");
            this.tr.Nodes.Add(tn);
            this.tn.Nodes.Add(tn1);
            this.Controls.Add(tr);
            this.tr.NodeMouseClick += new TreeNodeMouseClickEventHandler(tr_NodeMouseClick);
        }

        private void tr_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode clickedNode = e.Node;
            if (clickedNode.Checked)
            {
                if (clickedNode.Text.Equals("apple"))
                {
                    MessageBox.Show("you checked" + clickedNode.Text + "");
                }
                else if (clickedNode.Text.Equals("orange"))
                {
                    MessageBox.Show("you checked" + clickedNode.Text + "");
                }
            }
        }

    }
}
查看更多
登录 后发表回答