TreeView 高速单击时不运行AfterCheck时间
解决方法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时间的更多相关文章
- flex安装时停在计算时间界面的解决办法
现象:安装FLEX BUILDER4.6时停在计算时间界面,过了一会后弹出安装失败的对话框. 环境:WIN7 解决: 1.下载AdobeCreativeCloudCleanerTool, 地址:htt ...
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇 推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu 原文见:https://hom ...
- [转载]C / C++ 计算程序运行的时间
原文链接:https://blog.csdn.net/qq_36667170/article/details/79507547 在学数据结构过程中老师让查看不同算法的运行时间,然后让自己打印运行时间. ...
- 监测程序运行的时间,stopWatch
ArrayList arrInt = new ArrayList(); //用stopwatch来计时 运行的时间 Stopwatch watch = new Stopwatch(); watch.S ...
- input文本框去除单击时的边框的方法
前端开发写的input文本框标签后单击时可以看到有边框,去除边框的方法: input{ outline:medium; }
- 设置ASP.NET页面的运行超时时间详细到单个页面及站点
这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下 全局超时时间 服务器上如果有多个网站,希望统一设置一 ...
- vs2010运行C程序时,运行结果窗口一闪而过
摘要:vs2010运行C程序时,运行结果窗口一闪而过; ------------------------------------------------------------ Ctrl F5测试运行 ...
- EBS查找运行请求时间,参数等
--查找运行请求时间,参数等(可以是某用户的,某个报表) select c.user_name, papf.full_name, b.user_concurrent_program_name, a.r ...
- 正确理解java编译时,运行时以及构建时这三个概念
Java中的许多对象(一般都是具有父子类关系的父类对象)在运行时都会出现两种类型:编译时类型和运行时类型,例如:Person person = new Student();这行代码将会生成一个pers ...
随机推荐
- hadoop safemode error
http://www.cnblogs.com/hustcat/archive/2010/06/30/1768506.html 1.safemode bin/hadoop fs -put ./input ...
- Ubuntu 源码方式安装Subversion
使用到的安装包: apr-1.5.1.tar.gz apr-util-1.5.3.tar.gz pcre-8.35.tar.gz httpd-2.4.9.tar.bz2 subversion-1.8. ...
- Oracle 12c CDB PDB
先说基本用法: 先按11G之前进行 conn / as sysdba; create user test identifed by test; ORA-65096: 公用用户名或角色名无效. 查官方文 ...
- ORACLE检查死锁
一.数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错.二.死锁的原理当对于数据库某个表的某一列做更新或删除等操作,执行完毕后该条语句不提交,另一条对于这一列数据做更 ...
- java装箱拆箱
基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我们会这样: Class a = new Class(p ...
- Scala进阶之App特质
App特质的作用 App特质的作用那就是延迟初始化,从代码上看它继承自DelayedInit,里面有个delayedInit方法 trait App extends DelayedInit Delay ...
- Linux 禁止用户或 IP通过 SSH 登录
一切都是为了安全,做到来着可知! 限制用户 SSH 登录 1.只允许指定用户进行登录(白名单): 在 /etc/ssh/sshd_config 配置文件中设置 AllowUsers ...
- 转:变手把手教你玩转SOCKET模型之重叠I/O篇
手把手教你玩转SOCKET模型之重叠I/O篇 “身为一个初学者,时常能体味到初学者入门的艰辛,所以总是想抽空作点什么来尽我所能的帮助那些需要帮助的人.我也希望大家能把自己的所学和他人一起分享,不要去鄙 ...
- Android基于TCP的局域网聊天通信
概述 在同一局域网内,两台设备通过TCP进行通信聊天. 详细 代码下载:http://www.demodashi.com/demo/10567.html 一.准备工作 开发环境 jdk1.8 Ecli ...
- js 事件点击 显示 隐藏
<div style="position:absolute;left:40%;top:10%;border-style:dotted"> <img src=&qu ...