0

我在 VS 2012 中使用 .net c# 并尝试编写一个函数,该函数将自动检查已检查的父树视图节点的所有子节点。我是 c# 编程新手,所以我找到了以下代码,但它有两个问题:

  1. if (HasChildNodes(node)) Visual Studio 报告 HasChildNodes 的行是未知的。
  2. 我想从单击检查的选定节点开始,我认为代码遍历整个树?

谢谢你的帮助。

        treeView.BeginUpdate();
        //Loop through all the nodes of tree
        foreach (TreeNode node in treeView.Nodes)
        {
            //If node has child nodes
            if (HasChildNodes(node))
            {
                if (node.Checked == true)
                {
                    //Check all the child nodes.
                    foreach (TreeNode childNode in node.Nodes)
                    {
                        childNode.Checked = true;
                    }
                }
            }
        }
        treeView.EndUpdate();
4

7 回答 7

4

以下是检查和取消选中子节点和父节点的树视图示例的工作代码(添加树视图并将名称指定为 tvwDynamic)

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;

命名空间 Treeview { public 部分类 Form1 : Form { public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        TreeNode MainNode = new TreeNode("Java");
        tvwDynamic.Nodes.Add(MainNode);
        MainNode = new TreeNode("PHP");
        tvwDynamic.Nodes.Add(MainNode);           
        TreeNode node2 = new TreeNode("C#");
        TreeNode node3 = new TreeNode("VB.NET");
        TreeNode[] childNodes = new TreeNode[] {node2,node3};
        MainNode = new TreeNode("ASP.NET", childNodes);
        tvwDynamic.Nodes.Add(MainNode);
         TreeNode node4 = new TreeNode("Winforms");
        TreeNode node5 = new TreeNode("Webforms");
          TreeNode[] SubchildNodes = new TreeNode[] {node4,node5};
        MainNode =  new TreeNode("Test",SubchildNodes);
        tvwDynamic.Nodes[2].Nodes[1].Nodes.Add(MainNode);
        tvwDynamic.CheckBoxes = true;

    }

    private void tvwDynamic_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Action != TreeViewAction.Unknown)
        {

            if (e.Node.Nodes.Count > 0)
            {

                /* Calls the CheckAllChildNodes method, passing in the current 

                Checked value of the TreeNode whose checked state changed. */

                this.CheckAllChildNodes(e.Node, e.Node.Checked);

            }

        }

        SelectParents(e.Node, e.Node.Checked);

    }
    private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
    {

        foreach (TreeNode node in treeNode.Nodes)
        {

            node.Checked = nodeChecked;

            if (node.Nodes.Count > 0)
            {

                // If the current node has child nodes, call the CheckAllChildsNodes method recursively.

                this.CheckAllChildNodes(node, nodeChecked);

            }

        }

    }


    private void SelectParents(TreeNode node, Boolean isChecked)
    {
        var parent = node.Parent;

        if (parent == null)
            return;

        if (!isChecked && HasCheckedNode(parent))
            return;

        parent.Checked = isChecked;
        SelectParents(parent, isChecked);
    }

    private bool HasCheckedNode(TreeNode node)
    {
        return node.Nodes.Cast<TreeNode>().Any(n => n.Checked);
    }
} }
于 2014-10-05T18:01:15.160 回答
2
 treeview1.BeginUpdate();
 foreach (TreeNode tn in e.Node.Nodes)
     tn.Checked = e.Node.Checked;
 treeview1.EndUpdate();

这对我有用。您必须在 AfterCheck EventHandler 中添加此代码。

于 2017-09-04T12:25:01.183 回答
1

这是一个可以在 Visual Studio 的设计器中使用的简单控件:

public class BetterTreeView : TreeView
{
    /// <summary>
    /// Whether to apply Checked property changes to child nodes.
    /// </summary
    public bool TickChildNodes { get; set; }

    protected override void OnAfterCheck(TreeViewEventArgs e)
    {
        base.OnAfterCheck(e);

        if (TickChildNodes)
        {
            foreach (TreeNode node in e.Node.Nodes)
            {
                node.Checked = e.Node.Checked; // Triggers OnAfterCheck (recursive)
            }
        }
    }
}
于 2017-01-18T03:41:30.217 回答
0

没有任何HasChildNodes相关的方法TreeView,你在这里混淆了XML东西。

public void CheckNodes(TreeNode startNode){
   startNode.Checked = true;
   foreach(TreeNode node in startNode.Nodes)
      CheckNodes(node);       
}        
//then you can call the method above with the TreeNode you check
CheckNodes(yourNode);
于 2013-08-25T05:47:00.997 回答
0

检查您是否有子节点,检查就足够了

node.FirstNode != null

至于您的第二个问题,您的代码似乎检查了孔树,而不仅仅是从给定的节点,然后我再次需要查看整个方法签名以及您将按下的节点保留在哪里。如果你在一个成员中拥有它,myMember那么使用以下方法更改该treeView方法myMember

    treeView.BeginUpdate();
    //Loop through all the nodes of tree
    foreach (TreeNode node in myMember.Nodes)
    {
        //If node has child nodes
        if (node.FirstNode != null)
        {
            if (node.Checked == true)
            {
                //Check all the child nodes.
                foreach (TreeNode childNode in node.Nodes)
                {
                    childNode.Checked = true;
                }
            }
        }
    }
    treeView.EndUpdate();
于 2013-08-25T05:50:23.963 回答
0

更正foreach循环:

foreach (TreeNode node in treeView1.Nodes)
        {
            //If node has child nodes
            if (node.Checked == true)   //it is better to first check if it is "checked" then proceed to count child nodes
            {
                if (node.GetNodeCount(false) > 0)   //check if node has any child nodes
                {
                    //Check all the child nodes.
                    foreach (TreeNode childNode in node.Nodes)
                    {
                        childNode.Checked = true;
                    }
                }
            }
        }
于 2013-08-25T05:51:23.233 回答
0

检查父节点是检查..

然后遍历所有子节点..

if(e.Node.Checked==true)
            {
                foreach (TreeNode tn in e.Node.ChildNodes)
                {
                    tn.Checked = true;
                }
            }
于 2018-11-30T13:08:02.677 回答