想实现这些功能:1、父节点选取,所有子节点也被全部选取
2、父节点取消,所有子节点取消
3、子节点选取,对应父节点也被选取
但是只能3选2,为什么第3个始终不能实现?
以下为代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _9711
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "选择的部门:";
treeView1.CheckBoxes = true;
TreeNode TopNote = treeView1.Nodes.Add("公司");
TreeNode ParenetNote1 = new TreeNode("人事部");
TreeNode ParenetNote2 = new TreeNode("财务部");
TreeNode ParenetNote3 = new TreeNode("基础部");
TreeNode ParenetNote4 = new TreeNode("软件开发部");
TopNote.Nodes.Add(ParenetNote1);
TopNote.Nodes.Add(ParenetNote2);
TopNote.Nodes.Add(ParenetNote3);
TopNote.Nodes.Add(ParenetNote4);
TreeNode childNote1 = new TreeNode("c#部门");
TreeNode childNote2 = new TreeNode("ASP.NET部门");
TreeNode childNote3 = new TreeNode("VB部门");
TreeNode childNote4 = new TreeNode("VC部门");
TreeNode childNote5 = new TreeNode("Java部门");
TreeNode childNote6 = new TreeNode("PHP");
ParenetNote4.Nodes.Add(childNote1);
ParenetNote4.Nodes.Add(childNote2);
ParenetNote4.Nodes.Add(childNote3);
ParenetNote4.Nodes.Add(childNote4);
ParenetNote4.Nodes.Add(childNote5);
ParenetNote4.Nodes.Add(childNote6);
}
private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
{
if (e.Node.Text != "公司")
{ label1.Text = "选择的部门:" + e.Node.Text; }
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if ( e.Node.Checked == false)//父节点取消,所有子节点取消
{
foreach (TreeNode nd in e.Node.Nodes)
{ nd.Checked = false; }
}
else if (e.Node.Parent != null && e.Node.Checked == true)//子节点选取,对应父节点也被选取
{ e.Node.Parent.Checked = true; }
//else if (e.Node.Checked == true)//父节点选取,所有子节点也被全部选取
//{
// foreach (TreeNode nd in e.Node.Nodes)
// { nd.Checked = true; }
//}
}
}
}