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 ...
随机推荐
- MySQL auto_increment_increment 和 auto_increment_offset
参考这一篇文章:(不过我对这一篇文章有异议) http://blog.csdn.net/leshami/article/details/39779509 1:搭建测试环境 create table t ...
- linux安装JDK环境,JDK6.0即java 1.6.0
下载 (1)更改权限:# chmod 755 jdk-1.6.0_23-linux-i586.rpm.bin 执行安装:# ./ jdk-1.6.0_23-linux-i586.rpm.bin 此步完 ...
- iscsi initiator端Note
iscsi initiator端 (1)安装open-scsi sudo apt-get install open-iscsi open-iscsi-utils (2)cha ...
- J2EE项目集成SAP的BO报表
网上的方案: 每个用户在自己的J2EE系统的用户登陆的同时登陆bo系统,这做法的缺点是登陆bo速度慢,而且如果J2EE用户比较多的话会在bo服务器生成很多的token. 最佳方案(自己研究): 1.调 ...
- java Socket Udp聊天
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import jav ...
- Persona——Web人物角色介绍
一.什么是人物角色? 人物角色,即persona([pə:’səunə]),这里讨论的主要是web persona,是指针对网站目标群体真实特征的勾勒,是真实用户的综合原型.我们对产品使用者的目标.行 ...
- HDUOJ----(1031)Design T-Shirt
Design T-Shirt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hduoj---Tempter of the Bone
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- vsftp添加用户及测试
上一篇我们讲了vsftp安装以及配置,这篇我们讲下如何添加用户,然后我们测试一下,看看是否成功. 首先说下添加用户,如图执行命令即可: 这里简单解释一下:第一条命令是添加用户,第二条命令是设置用户密码 ...