带有检查节点递归的 WinForms TreeView [英] WinForms TreeView With Checked Nodes Recursion

查看:28
本文介绍了带有检查节点递归的 WinForms TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决我认为应该是我的 Treeview 的基本递归的问题,但我似乎无法获得正确的逻辑.

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

本质上我有一个 WinForms Treeview(C# Net Framework 4.5.1),它有一个节点列表,这些节点是标准节点(没有自定义控件),每个节点都启用了复选框.

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.

一些例子如下:-

  • 节点 1 (X)
    • 节点 2 (X)
      • 节点 3 (X)
      • 节点 4 (X)
      • 节点 5 (X)

      这个例子将返回节点 1,因为它的所有子节点(和后续子节点)都被检查

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

      • 节点 1 (X)
        • 节点 2 (X)
          • 节点 3 (X)
          • 节点 4
          • 节点 5 (X)

          这将返回节点 1/节点 2/节点 3/节点 5

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

          • 节点 1 (X)
            • 节点 2 (X)
              • 节点 3
              • 节点 4
              • 节点 5 (X)
                • 节点 6 (X)
                • 节点 7 (X)

                这将返回节点 1/节点 2/节点 5

                This would return Node 1 / Node 2 / Node 5

                • 节点 1 (X)
                  • 节点 2 (X)
                    • 节点 3
                    • 节点 4 (X)
                    • 节点 5 (X)
                      • 节点 6
                      • 节点 7 (X)

                      这将返回节点 1/节点 2/节点 4/节点 5/节点 7

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

                      任何帮助将不胜感激.

                      谢谢.

                      推荐答案

                      嗨执事凯利,你可以使用这个方法.

                      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 + "");
                                      }
                                  }
                              }
                      
                          }
                      }
                      

                      这篇关于带有检查节点递归的 WinForms TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆