首先事件选择,选择的是MouseUp事件。为啥?因为凡是跟Check有关的,在选中父节点或者子节点,都会二次触发。然后发生的就是死循环。

Up事件就可以避免二次触发。Down事件呢?那就触发AfterCheck事件了。事件选好了, 直接上代码。

处理思路:选中/取消当前节点,先选中其所有父节点,再选中其子节点

注意平级节点处理:有平级节点选中,取消时需要遍历父节点。

若有一个平级节点处于选中,则父节点为选中。

若所有平级都没有选中的了,则父节点要取消选中

        /// <summary>
/// 处理树节点选中和取消选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void treeView_MouseUpClick(object sender,MouseEventArgs e)
{
TreeView tree = sender as TreeView;
if (tree == null)
return;
TreeNode node = tree.GetNodeAt(e.X, e.Y);
if (node == null)
return;
SetTreeNodeCheckBoxState(node,true,node.Checked);
SetTreeNodeCheckBoxState(node, false, node.Checked);
} /// <summary>
/// 处理父节点,子节点的选中
/// </summary>
/// <param name="node">需要选中的树</param>
/// <param name="isSetParentState">是否选中父节点</param>
/// <param name="state">选中/不选中</param>
public static void SetTreeNodeCheckBoxState(TreeNode node, bool isSetParentState, bool state)
{
if (node == null)
return;
node.Checked = state;
if (isSetParentState && node.Parent != null && node.Checked==true)
{
SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
}
else if (isSetParentState && node.Parent != null && node.Checked == false)
{
//处理取消选中
bool isSelect = false;
foreach(TreeNode n in node.Parent.Nodes)
{
if (n.Checked==true)
isSelect = true;
}
//平级都未有选中的才取消选中
if(isSelect==false)
SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
}
else if (!isSetParentState && node.Nodes!=null && node.Nodes.Count > )
{
foreach (TreeNode node2 in node.Nodes)
{
SetTreeNodeCheckBoxState(node2, isSetParentState, state);
}
}
}

TreeView树,全选,反选,平级选操作的更多相关文章

  1. jQuery实现checkbox全选反选及删除等操作

    1.list.html 说明:用checkbox数组Check[]存放每一行的ID值 <div id="con"> <table width="100% ...

  2. jQuery之标签操作和返回顶部、登录验证、全选反选、克隆示例

    一.样式操作 1.JQ中的样式类 somenode.addClass();// 添加指定的CSS类名. somenode.removeClass();// 移除指定的CSS类名. somenode.h ...

  3. html中全选反选

    <!--第一层--> <div class="first"> <div class="first_top"> <img ...

  4. JavaScript、全选反选-课堂笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 关于input全选反选恶心的异常情况

    上一篇讲到:第一次点击全选按钮input显示对勾,第二次则不显示,需要用prop来添加checked属性. 但是用prop会出现一个问题,对勾显示,而checked属性不会被添加(比如:$(" ...

  6. jQuery全选/反选checkbox

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. python: jquery实现全选 反选 取消

    引入这个jquery-1.12.4.js jquery实现全选 反选 取消 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitio ...

  8. BootStrapt iCheck表单美化插件使用方法详解(含参数、事件等) 全选 反选

    特色: 1.在不同浏览器(包括ie6+)和设备上都有相同的表现 — 包括 桌面和移动设备 2.支持触摸设备 — iOS.Android.BlackBerry.Windows Phone等系统 4.方便 ...

  9. jquery 全选 全不选 反选

    1.概述 在项目中经常遇到列表中对复选框进行勾选操作,全选...反选.. 2. example <html> <body> <form id="test-for ...

随机推荐

  1. Prometheus(三):Prometheus监控交换机(snmp)

    默认已安装Prometheus服务,服务地址:192.168.56.200 一.获取交换机snmp信息 snmp服务IP(交换机IP):172.20.2.83 snmp community:dfete ...

  2. shell 脚本中 while 只执行一次

    实例代码 while read line ; do ssh -p20002 $line -o StrictHostKeyChecking=no xxxxxxxxx done < ip.txt w ...

  3. https工具类

    import org.apache.commons.lang.StringUtils; import javax.net.ssl.*; import java.io.*; import java.ne ...

  4. BareTail(日志查看工具)

    官网:http://www.baremetalsoft.com/baretail/index.php 在看log文件时,当日志有新增时,会自动滚动到最新的那一行,对于查看实时日志有作用.

  5. 17 个方面,综合对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 四个分布式消息队列

    原文:https://mp.weixin.qq.com/s/lpsQ3dEZHma9H0V_mcxuTw 一.资料文档 二.开发语言 三.支持的协议 四.消息存储 五.消息事务 六.负载均衡 七.集群 ...

  6. python 解决粘包问题的例子(ftp文件的上传与下载)简单版本

    服务端 ! /user/bin/env python3 -- coding:utf_8 -- """ Author:Markli # 2019/9/9,16:41 &qu ...

  7. NOIP 2006 明明的随机数

    洛谷 P1059 明明的随机数 洛谷传送门 JDOJ 1423: [NOIP2006]明明的随机数 T1 JDOJ传送门 Description 明明想在学校中请一些同学一起做一项问卷调查,为了实验的 ...

  8. [51nod1227]平均最小公倍数(莫比乌斯反演+杜教筛)

    题意 求 $\sum_{i=a}^b \sum_{j=1}^i \frac{lcm(i,j)}{i}$. 分析 只需要求出前缀和, $$\begin{aligned}\sum_{i=1}^n \sum ...

  9. UFUN函数 UF_UI UF_PART函数(UF_UI_select_with_class_dialog, UF_PART_export_with_options)

    /*主要演示 UF_PART_export_with_options 这个函数 */1 //设置class_dialog选择过滤 static int init_proc(UF_UI_selectio ...

  10. PATA1055 The World's Richest (25 分)

    1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires based ...