using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace CnTreeView
{
public static class TreeViewCheck
{
/// <summary>
/// 系列节点 Checked 属性控制
/// </summary>
/// <param name="e"></param>
public static void CheckControl(TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node != null)
{
CheckParentNode(e.Node, e.Node.Checked);
}
if (e.Node.Nodes.Count > 0)
{
CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
#region 私有方法
//改变所有子节点的状态
private static void CheckAllChildNodes(TreeNode pn, bool IsChecked)
{
foreach (TreeNode tn in pn.Nodes)
{
tn.Checked = IsChecked;
if (tn.Nodes.Count > 0)
{
CheckAllChildNodes(tn, IsChecked);
}
}
}
//改变父节点的选中状态
private static void CheckParentNode(TreeNode curNode, bool IsChecked)
{
bool bChecked = true;
if (curNode.Parent != null)
{
foreach (TreeNode node in curNode.Parent.Nodes)
{
if (node.Checked == false)
{
bChecked = false;
break;
}
}
if (bChecked)
{
curNode.Parent.Checked = true;
CheckParentNode(curNode.Parent, true);
}
else
{
curNode.Parent.Checked = false;
CheckParentNode(curNode.Parent, false);
}
}
}
#endregion
}
}
TreeViewCheck
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Xml;
using System.IO;
using System.Threading;
namespace CnTreeView
{
public partial class TreeViewDemo : Form
{
private XmlDocument docXML = new XmlDocument();
public TreeViewDemo()
{
InitializeComponent();
this.mixedCheckBoxesTreeView1.BeforeCheck += new System.Windows.Forms.TreeViewCancelEventHandler(this.myTreeView1_BeforeCheck);
this.mixedCheckBoxesTreeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.myTreeView1_AfterCheck);
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
}
private SelectUser selectUser;
#region 树形控件 单选
// (一)1、 单选树形控件 BeforeCheck
private void myTreeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
TreeNode oldNode = this.mixedCheckBoxesTreeView1.Tag as TreeNode;
if (oldNode != null && oldNode != e.Node)
{
oldNode.Checked = false;
}
TreeNodeCollection nodes = this.mixedCheckBoxesTreeView1.Nodes;
if (e.Node.Parent != null)
{
nodes = e.Node.Parent.Nodes;
}
foreach (TreeNode node in nodes)
{
if (node != e.Node)
{
node.Checked = false;
}
}
this.mixedCheckBoxesTreeView1.Tag = e.Node;
}
}
// (一)2、 单选树形控件 AfterCheck
//遍历 获取选中的节点的值
private void myTreeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
foreach (TreeNode node in mixedCheckBoxesTreeView1.Nodes)
{
SetTreeViewList(node);
}
label3.Text = "接收对象" + listBox1.Items.Count.ToString() + "人";
}
//(一)、3 SetTreeViewList 遍历TreeView 设置ListBox中的内容
public void SetTreeViewList(TreeNode nodes)
{
//防止节点没有子节点被忽略
if (nodes.Checked && nodes.Nodes.Count == 0)
{
listBox1.Items.Clear();
selectUser = new SelectUser(nodes.Text, nodes.Name);
listBox1.Items.Add(selectUser);
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "UserID";
}
//有子节点
foreach (TreeNode newNode in nodes.Nodes)
{
if (newNode.Nodes.Count > 0)
{
SetTreeViewList(newNode);
}
//判断孩子节点
else
{
if (newNode.Checked && newNode.Nodes.Count == 0)
{
listBox1.Items.Clear();
selectUser = new SelectUser(newNode.Text, newNode.Name);
listBox1.Items.Add(selectUser);
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "UserID";
}
}
}
}
#endregion
#region 树形控件 多选
//level的作用主要是为了防止触发子节点的AfterCheck方法。
private int level = 1;
//遍历 获取选中的节点的值
//(二) 1、 多选树形控件 AfterCheck
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (level == 1)
{
level = 2;
//遍历选择所有相关子栏目
TreeViewCheck.CheckControl(e);
listBox1.Items.Clear();
//遍历所有子栏目并添加到listBox中
foreach (TreeNode node in treeView1.Nodes)
{
SetMulTreeViewList(node);
}
label3.Text = "接收对象" + listBox1.Items.Count.ToString() + "人";
}
}
//(二) 2、 设置多个树形控件列表
public void SetMulTreeViewList(TreeNode nodes)
{
level = 1;
//防止节点没有子节点被忽略
if (nodes.Checked && nodes.Nodes.Count == 0)
{
selectUser = new SelectUser(nodes.Text, nodes.Name);
if (!hasExist(selectUser))
{
listBox1.Items.Add(selectUser);
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "UserID";
}
}
//有子节点
foreach (TreeNode newNode in nodes.Nodes)
{
if (newNode.Nodes.Count > 0)
{
SetMulTreeViewList(newNode);
}
//判断孩子节点
else
{
if (newNode.Checked && newNode.Nodes.Count == 0)
{
selectUser = new SelectUser(newNode.Text, newNode.Name);
if (!hasExist(selectUser))
{
listBox1.Items.Add(selectUser);
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "UserID";
}
}
}
}
}
//(二) 3. 添加的项目是否已经存在
private bool hasExist(SelectUser selectUser)
{
bool flag = false;
foreach (SelectUser _Row in listBox1.Items)
{
if (_Row.Name == selectUser.Name && _Row.UserID == selectUser.UserID)
flag = true;
}
return flag;
}
#endregion
#region 树形控件
private void radioButton2_Click(object sender, EventArgs e)
{
mixedCheckBoxesTreeView1.Visible = false;
treeView1.Visible = true;
listBox1.Items.Clear();
label3.Text = "接收对象0人";
level = 2;
CheckTreeview(treeView1, false);
level = 1;
}
private void radioButton1_Click(object sender, EventArgs e)
{
mixedCheckBoxesTreeView1.Visible = true;
treeView1.Visible = false;
listBox1.Items.Clear();
label3.Text = "接收对象0人";
CheckTreeview(mixedCheckBoxesTreeView1, false);
}
//遍历TreeView 全部取消 或全部选中
private void checkSubNodes(TreeNode treeNode, bool flag)
{
treeNode.Checked = flag;
foreach (TreeNode tn in treeNode.Nodes)
{
checkSubNodes(tn, flag);
}
}
private void CheckTreeview(TreeView treeView, bool flag)
{
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
checkSubNodes(n, flag);
}
}
#endregion
#region 加载xml菜单
private void LoadMixedTreeView()
{
mixedCheckBoxesTreeView1.Nodes.Clear(); // Clear any existing items
mixedCheckBoxesTreeView1.BeginUpdate(); // Begin updating the treeview
HiddenCheckBoxTreeNode rootNode = new HiddenCheckBoxTreeNode("某某单位");
mixedCheckBoxesTreeView1.Nodes.Add(rootNode);
HiddenCheckBoxTreeNode treenode;
XmlNodeList baseNodeList = docXML.SelectNodes("根目录/组"); // Get all first level <folder> nodes
foreach (XmlNode xmlnode in baseNodeList) // loop through all base <folder> nodes
{
string title = xmlnode.Attributes["名称"].Value;
string id = xmlnode.Attributes["用户ID"].Value;
treenode = new HiddenCheckBoxTreeNode(title); // add it to the tree
treenode.Name = id;
rootNode.Nodes.Add(treenode);
populateChildNodes(xmlnode, treenode); // Get the children
}
mixedCheckBoxesTreeView1.EndUpdate(); // Stop updating the tree
mixedCheckBoxesTreeView1.Refresh();
this.mixedCheckBoxesTreeView1.Nodes[0].Expand();
}
private void LoadTreeView()
{
treeView1.Nodes.Clear(); // Clear any existing items
treeView1.BeginUpdate(); // Begin updating the treeview
TreeNode rootNode = new TreeNode("某某单位");
treeView1.Nodes.Add(rootNode);
TreeNode treenode;
XmlNodeList baseNodeList = docXML.SelectNodes("根目录/组"); // Get all first level <folder> nodes
foreach (XmlNode xmlnode in baseNodeList) // loop through all base <folder> nodes
{
string title = xmlnode.Attributes["名称"].Value;
string id = xmlnode.Attributes["用户ID"].Value;
treenode = new TreeNode(title); // add it to the tree
treenode.Name = id;
rootNode.Nodes.Add(treenode);
populateChildNodes(xmlnode, treenode); // Get the children
}
treeView1.EndUpdate(); // Stop updating the tree
treeView1.Refresh();
this.treeView1.Nodes[0].Expand();
}
private void populateChildNodes(XmlNode oldXmlnode, TreeNode oldTreenode)
{
//TreeNode treenode = null;
XmlNodeList childNodeList = oldXmlnode.ChildNodes; // Get all children for the past node (parent)
foreach (XmlNode xmlnode in childNodeList) // loop through all children
{
string title = xmlnode.Attributes["名称"].Value;
string id = xmlnode.Attributes["用户ID"].Value;
TreeNode tnode = new TreeNode(title);
tnode.Name = id;
oldTreenode.Nodes.Add(tnode);
//treenode = oldTreenode.Nodes.Add(title); // add it to the parent node tree
populateChildNodes(xmlnode, tnode); // get any children for this node
}
}
#endregion
private void TreeViewDemo_Load(object sender, EventArgs e)
{
docXML.Load(AppDomain.CurrentDomain.BaseDirectory + "\\menu.xml");
LoadMixedTreeView();
LoadTreeView();
}
}
}
TreeViewDemo