解决方法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. Linux下时间格式转换及获取方法

    Linux下使用clock_gettime给程序计时 #include <stdio.h> #include <unistd.h> #include <stdlib.h& ...

  2. HTTP和Socket的区别

    1: HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用. HTTP ...

  3. 微信小程序 - 上拉加载下拉刷新

    点击下载示例:小程序 - 上拉刷新下拉加载

  4. Java从零开始学五(数据类型转换)

    一.数据类型转换 分为“自动类型转换”和“强制类型转换” 二.自动类型转换 低级别------>高级别 byte b=7; int i=b; System.out.println("i ...

  5. 触发器二(DML触发器)(学习笔记)

    DML触发器(语句触发器) 由DML语句进行触发,当用户执行了INSERT,UPDATE,DELETE操作时就会触发操作 示例一.只有在每个月的10日才允许办理,新员工入职与离职,其他时间不允许增加和 ...

  6. repr

    >>> import datetime >>> today=datetime.datetime.now() >>> today datetime. ...

  7. 安装到LG手机出错

    [2013-07-10 07:44:31 - txrjsms] ERROR: Application requires API version 11. Device API version is 8 ...

  8. Apache-一个IP多个主机域名

    #配置虚拟主机名 NameVirtualHost 192.168.209.128 <VirtualHost 192.168.209.128> DocumentRoot /htdocs/wi ...

  9. beanUtils的用法

    举例1:使用BeanUtils工具封装用户提交的数据. public static void main(String[] args)throws Exception { // 模拟用户的输入的数据如下 ...

  10. windows named pipe 客户端 服务器

    可以实现多客户端对一服务端,服务端为客户端提供服务. 其实一服务端对应每一个client pipe都新建立了一个pipe.windows允许建立多个同名pipe 效果: 服务端代码: #define ...