在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 && !Convert.IsDBNull(e.Node)) { CheckParentNode(e.Node); 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 bChecked = false; if (curNode.Parent != null) { foreach (TreeNode node in curNode.Parent.Nodes) { if (node.Checked) { bChecked = true; break; } } if (bChecked) { curNode.Parent.Checked = true; CheckParentNode(curNode.Parent); } else { curNode.Parent.Checked = false; CheckParentNode(curNode.Parent); } } } #endregion } 调用方法:在treeview的AfterCheck事件中 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) } 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 && !Convert.IsDBNull(e.Node)) { CheckParentNode(e.Node); 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 bChecked = false; if (curNode.Parent != null) { foreach (TreeNode node in curNode.Parent.Nodes) { if (node.Checked) { bChecked = true; break; } } if (bChecked) { curNode.Parent.Checked = true; CheckParentNode(curNode.Parent); } else { curNode.Parent.Checked = false; CheckParentNode(curNode.Parent); } } } #endregion } 调用方法:在treeview的AfterCheck事件中 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论