我的Windows窗体应用程序中有一个带有复选框的树视图控件。如果选择或取消选择其父节点,如何自动选择或取消选择所有子节点?
答案 0 :(得分:12)
如果我没有弄错的话,AfterCheck事件文档中的示例代码完全符合您的要求。
答案 1 :(得分:11)
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);
}
}
}
答案 2 :(得分:1)
//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....
答案 3 :(得分:1)
使用树视图的 AfterCheck 事件来编码树视图中子节点应该发生的事情。
将树视图节点的相同状态传递给其子节点,并将其“已检查”属性设置为树视图节点的相同状态。
答案 4 :(得分:1)
这个简单的代码适合我
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);
}
}
}
答案 5 :(得分:1)
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;
}
}
}
答案 6 :(得分:0)
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
答案 7 :(得分:0)
尝试使用此代码,以便在单击子节点时检查和取消选中父节点。
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;
}
}
答案 8 :(得分:0)
请找到下面的工作代码。我在我的申请中使用这个。希望这能解决问题。
private void tree_Documents_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeNode checkedNode = e.Node;
foreach (TreeNode node in checkedNode.Nodes)
{
node.Checked = checkedNode.Checked;
}
}
答案 9 :(得分:0)
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;
}
}