解决方法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. VMware12 中安装MS-DOS 7.10

    按一下步骤进行安装: 选择虚拟机,然后如下图选择“ 编辑虚拟机设置 ”. 弹出的编辑框中,选择“CD/DVD”中的“使用ISO镜像文件”,然后选择“浏览”,打开MS-DOS7.10.iso的ISO镜像 ...

  2. 算法笔记_127:蓝桥杯2017模拟赛-本科组习题解答(Java)

     目录 1 算年龄 2 猜算式 3 排列序数 4 字符串比较 5 还款计算 6 滑动解锁 7 风险度量   PS:以下代码部分仅供参考,若有不当之处,还请路过同学指出哦~ 1 算年龄 标题:算年龄 英 ...

  3. rpm常用命令及rpm参数介绍

    RPM是RedhatPackageManager的缩写,是由RedHat公司开发的软件包安装和管理程序,同Windows平台上的Uninstaller比较类似.使用RPM,用户可以自行安装和管理Lin ...

  4. PHPstorm自定义快捷键

    Ctrl+alt+S 打开设置 PHPstorm 设置 PHPstorm 主题安装 自定义快捷键设置 ·全屏 F11 ·另外一种全屏alt+F11 Database数据库管理 alt+d Termin ...

  5. JProfiler_SN_8_x key

    按默认选择“Single or evaluation license”Name 和 Company 随意-----------------------忧郁的分割线------------------- ...

  6. 使用Nginx限制同一IP的访问频率

    http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html http://nginx.org/en/docs/http/ngx_http ...

  7. 转载【微信小程序】:微信小程序滚动Tab选项卡:左右可滑动切换(仿某宝)

    1.本文转载至:http://blog.csdn.net/sophie_u/article/details/71745125 2.效果: 3.最终效果如上.问题: 1).tab标题总共8个,所以一屏无 ...

  8. input text的所有事件

    onBlur 当 text field失去聚焦的时候执行一些代码 onChange 当 text field失去聚焦并且它的值发生变动的时候执行一些代码 onClick 当用户在 text field ...

  9. Qt WebView改造成 QML App

    这是去年的一个项目,虽然研究出来了,解了一时之需,但随后束之高阁.当时Qt的版本是4.8.现在整理如下: 把QT HTML5 APP改造成 QML App 方案 新建一个QML自定义控件,该控件包含Q ...

  10. Win8.1设置ftp服务器并设定用户操作权限的详细教程

    http://wenku.baidu.com/link?url=VTDLnDa_yfQN9OldjVnYsOBf7UdIj76QjaLDyHP-I0A6iFEfzB8EyBf9uztwm2JDXlFL ...