I have one Tree View Control with check boxes in my Windows Forms Ap开发者_如何转开发plication.How to automatically select or deselect the all child nodes whenever its parent Node is selected or deselected?
If I'm not mistaken, the sample code in the documentation for the AfterCheck event does exactly what you want.
private void HandleOnTreeViewAfterCheck(Object sender,
TreeViewEventArgs e)
{
CheckTreeViewNode(e.Node, e.Node.Checked);
}
private void CheckTreeViewNode(TreeNode node, Boolean isChecked)
{
foreach (TreeNode item in node.Nodes)
{
item.Checked = isChecked;
if (item.Nodes.Count > 0)
{
this.CheckTreeViewNode(item, isChecked);
}
}
}
//TreeView - myTreeview;
myTreeview.BeginUpdate();
//Loop through all the nodes of tree
foreach (TreeNode node in myTreeview.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;
}
}
}
}
myTreeview.EndUpdate();
Hope this helps....
Use AfterCheck event of the treeview to code for what should happen to the child nodes inside the treeview.
pass the same state of the tree view node to its child also and set its "checked" property to the same state of the treeview node.
Pleas find the working code below. I am using this in my application. Hope this will solve the issue.
private void tree_Documents_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeNode checkedNode = e.Node;
foreach (TreeNode node in checkedNode.Nodes)
{
node.Checked = checkedNode.Checked;
}
}
This simple code works for me
private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
try {
e.Node.TreeView.BeginUpdate();
if (e.Node.Nodes.Count > 0) {
var parentNode = e.Node;
var nodes = e.Node.Nodes;
CheckedOrUnCheckedNodes(parentNode, nodes);
}
} finally {
e.Node.TreeView.EndUpdate();
}
}
private void CheckedOrUnCheckedNodes(TreeNode parentNode, TreeNodeCollection nodes) {
if (nodes.Count > 0) {
foreach (TreeNode node in nodes) {
node.Checked = parentNode.Checked;
CheckedOrUnCheckedNodes(parentNode, node.Nodes);
}
}
}
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if (node.Nodes.Count > 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
private void treeView_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.Nodes.Count > 0)
{
if (!e.Node.Checked)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
if (e.Node.Parent != null)
{
TreeNode n = e.Node;
while (n.Parent != null)
{
if (n.Checked)
{
n.Parent.Checked = true;
}
n = n.Parent;
}
}
}
If Not TreeView1.Nodes(e.Node.Text) Is Nothing Then
For Each node As TreeNode In TreeView1.Nodes(e.Node.Text).Nodes
If node.Parent.Checked = True Then
node.Checked = True
End If
If node.Parent.Checked = False Then
node.Checked = False
End If
Next
End If
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Nodes.Count != 0)
{
if (e.Node.Checked)
{
CheckAllChildNodes1(e.Node);
}
else
{
UncheckChildNodes1(e.Node);
}
}
}
private void CheckAllChildNodes1(TreeNode treeNodeAdv)
{
foreach (TreeNode nd in treeNodeAdv.Nodes)
{
nd.Checked = treeNodeAdv.Checked;
}
}
private void UncheckChildNodes1(TreeNode treeNodeAdv)
{
foreach (TreeNode nd in treeNodeAdv.Nodes)
{
nd.Checked = treeNodeAdv.Checked;
}
}
Try this code, this for check and uncheck parent node as well on click on child node.
private void CheckTreeViewNode(TreeNode node, Boolean isChecked)
{
foreach (TreeNode item in node.Nodes)
{
item.Checked = isChecked;
if (item.Nodes.Count > 0)
{
this.CheckTreeViewNode(item, isChecked);
}
}
if (isChecked && node.Parent != null)
{
treeView1.AfterCheck -= HandleOnTreeViewAfterCheck;
node.Parent.Checked = true;
treeView1.AfterCheck += HandleOnTreeViewAfterCheck;
}
if (!isChecked && node.Parent != null)
{
bool unchk = true;
treeView1.AfterCheck -= HandleOnTreeViewAfterCheck;
foreach (TreeNode item in node.Parent.Nodes)
{
if (item.Checked)
{
unchk = false;
break;
}
}
if (unchk)
node.Parent.Checked = false;
treeView1.AfterCheck += HandleOnTreeViewAfterCheck;
}
}
精彩评论