解决方法1:

在AfterCheck事件中,通过System.Threading.Thread.Sleep()来控制函数的运行的最短时间。保证函数运行时间必须大于某个值

解决方法2:

编写列TreeView2

class
TreeView2: TreeView 

     {

        protected override void WndProc(ref Message m)

        {

            if(m.Msg!=0x203)

            {

                base.WndProc(ref m);

            }

        }

    }

To reproduce this, start a C#.NET Windows Forms application, drop a TreeView control onto the form and set the CheckBoxes property to true.  Then add an AfterCheck event and paste in this over the Form1 code:

using System;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

private void Form1_Load(object sender, EventArgs e)

        {

            TreeNode parent = treeView1.Nodes.Add("Parent");

            parent.Nodes.Add("Child1");

            parent.Nodes.Add("Child2");

            parent.ExpandAll();

        }

private void TreeView1AfterCheck(object sender, TreeViewEventArgs e)

        {

            if (e.Action == TreeViewAction.Unknown)

                return;

//If the parent is clicked then set the checkstate of the children to match

            if (e.Node.Level == 0)

                foreach (TreeNode childNode in e.Node.Nodes)

                    childNode.Checked = e.Node.Checked;

//If a child is clicked then set the parent to be checked if any children are checked otherwise unchecked

            else if (e.Node.Level == 1)

            {

                TreeNode parent = e.Node.Parent;

                bool noneChecked = true;

                foreach (TreeNode siblingNode in parent.Nodes)

                    if (siblingNode.Checked)

                        noneChecked = false;

                parent.Checked = !noneChecked;

            }

        }

    }

}

When you run the application you will see a form with a tree view with a Parent and two Children.  When you check/ uncheck the Parent the Children will check/ uncheck.  Unchecking all the children will uncheck the Parent and checking either Child will check
the Parent if it isn't already checked.

This works fine if you click slowly or use the keyboard.

However, if you click the Parent twice quickly (but not quickly enough to be a double-click) then the Tree View doesn't fire the AfterCheck event the second time and goes into some weird state where the form freezes up until you click on it (that click is then
ignored).

For example, if I have everything checked and click the Parent twice quickly I will see all the checkboxes clear and then JUST the Parent will end up checked (even though this should be impossible).  At this point hovering the Form will not show where the mouse
has focus and the next click will be ignored!  For example, you can click the close button and the form will remain open but will start to work properly again.

I tried to debug this, it looks as if the NodeMouseClick event does fire both times but the AfterCheck event only fires the first time.

NOTE: this only happens when using the mouse, using the keyboard is fine, you can tap away on the space bar as fast as you like and it always works.

Answer:

The .NET TreeView class heavily customizes mouse handling for the native Windows control in order to synthesize the Before/After events. Unfortunately, they didn't get it quite right. When you start clicking fast, you'll generate double-click messages. The
native control responds to a double-click by toggling the checked state for the item, without telling the .NET wrapper about it. You won't get a Before/AfterCheck event.

It's a bug but they won't fix it. The workaround is not difficult, you'll need to prevent the native control from seeing the double-click event. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the
toolbox, replacing the existing one.

using System;
using System.Windows.Forms; class MyTreeView : TreeView {
protected override void WndProc(ref Message m) {
// Filter WM_LBUTTONDBLCLK
if (m.Msg != 0x203) base.WndProc(ref m);
}
}

TreeView 高速单击时不运行AfterCheck时间的更多相关文章

  1. flex安装时停在计算时间界面的解决办法

    现象:安装FLEX BUILDER4.6时停在计算时间界面,过了一会后弹出安装失败的对话框. 环境:WIN7 解决: 1.下载AdobeCreativeCloudCleanerTool, 地址:htt ...

  2. Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇   推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu  原文见:https://hom ...

  3. [转载]C / C++ 计算程序运行的时间

    原文链接:https://blog.csdn.net/qq_36667170/article/details/79507547 在学数据结构过程中老师让查看不同算法的运行时间,然后让自己打印运行时间. ...

  4. 监测程序运行的时间,stopWatch

    ArrayList arrInt = new ArrayList(); //用stopwatch来计时 运行的时间 Stopwatch watch = new Stopwatch(); watch.S ...

  5. input文本框去除单击时的边框的方法

    前端开发写的input文本框标签后单击时可以看到有边框,去除边框的方法: input{     outline:medium; }

  6. 设置ASP.NET页面的运行超时时间详细到单个页面及站点

    这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下     全局超时时间 服务器上如果有多个网站,希望统一设置一 ...

  7. vs2010运行C程序时,运行结果窗口一闪而过

    摘要:vs2010运行C程序时,运行结果窗口一闪而过; ------------------------------------------------------------ Ctrl F5测试运行 ...

  8. EBS查找运行请求时间,参数等

    --查找运行请求时间,参数等(可以是某用户的,某个报表) select c.user_name, papf.full_name, b.user_concurrent_program_name, a.r ...

  9. 正确理解java编译时,运行时以及构建时这三个概念

    Java中的许多对象(一般都是具有父子类关系的父类对象)在运行时都会出现两种类型:编译时类型和运行时类型,例如:Person person = new Student();这行代码将会生成一个pers ...

随机推荐

  1. php之快速入门学习-1

    貌似和JSP差不多! PHP 脚本可以放在文档中的任何位置. PHP 脚本以 <?php 开始,以 ?> 结束: <?php// PHP 代码?> PHP 文件的默认文件扩展名 ...

  2. 笔试题之javaweb

    Java web部分 1.Tomcat的优化经验      答:去掉对web.xml的监视,把jsp提前编辑成Servlet.      有富余物理内存的情况,加大tomcat使用的jvm的内存 2. ...

  3. ionic 项目中 使用 sass

    注: 1.先安装node-sass  -->> npm install --save node-sass --registry=https://registry.npm.taobao.or ...

  4. IE hack 汇总

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

  5. HTTP请求响应码

    ① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 203 非认证信息 204 无内容 205 重置内容 206 部分内容 ③ 重定向 300 多路选 ...

  6. 〖Android〗/system/etc/event-log-tags

    一博客中,一段的注释的解释:[From: http://blog.csdn.net/hustpzb/article/details/8525324] /** * Access to the syste ...

  7. 通俗的理解HTTPS以及SSL中的证书验证

    一.HTTPS的安全性体现在哪 HTTP(超文本传输协议,Hyper Text Transfer Protocol)是我们浏览网站信息传输最广泛的一种协议.HTTPS(Hyper Text Trans ...

  8. npm下载缓慢解决方法

    npm的服务器在国外,拉取npm包的列表.下载包这个过程会比较缓慢.凡是包管理工具基本都有这个问题,例如maven.pip等,这些问题都可以通过配置镜像来解决.阿里巴巴提供了maven库,清华大学有p ...

  9. 直线的中点Bresenham算法的实现

    一.实验目的 1.掌握在MFC中搭建图形绘制的基本框架的方法: 2.将直线的中点Bresenham算法转化成可执行代码. 二.实验内容 1. 通过分析具体数据在中点Bresenham算法上的执行过程, ...

  10. 推荐6款极具个性化的在线生成logo的网站

    http://www.douban.com/group/topic/10434724/