我在VS 2012中使用.net c#并尝试编写一个自动检查已检查父树视图节点的所有子节点的函数。我是c#编程的新手,所以我发现了以下代码,但它有两个问题:
感谢您的帮助。
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();
答案 0 :(得分: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 { 公共部分类Form1:表格 { 公共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);
}
} }
答案 1 :(得分:1)
这是一个可以在Visual Studio的Designer中使用的简单控件:
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)
}
}
}
}
答案 2 :(得分:1)
treeview1.BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
tn.Checked = e.Node.Checked;
treeview1.EndUpdate();
这对我有用。你必须在AfterCheck EventHandler中添加这段代码。
答案 3 :(得分: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);
答案 4 :(得分: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();
答案 5 :(得分: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;
}
}
}
}
答案 6 :(得分:0)
检查父节点是否被检查。
然后遍历所有子节点。
if(e.Node.Checked==true)
{
foreach (TreeNode tn in e.Node.ChildNodes)
{
tn.Checked = true;
}
}