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 ...
随机推荐
- Silverlight 之 浅析
一.silverlight定义及作用 silverlight用XAML来做前端界面,用.NET或者JS作为程序脚本支持,在浏览器内外运行的应用.可以认为和FLASH 和ADOBE AIR有很大的功能重 ...
- Ubuntu系统环境变量配置文件(转)
原文:http://www.cnblogs.com/eastson/archive/2012/06/15/2550151.html 在Ubuntu中有如下几个文件可以设置环境变量: /etc/prof ...
- Java从零开始学十九(异常)
一.什么是异常 从字面上讲,就是不正常的现实就是异常. 程序中的异常也是要在程序运行中才会偶尔发生.如果程序还没有运行,编译就报错,这种不叫异常,这种叫编译错误,通常是语法上的错误 二.java中异常 ...
- kafka delete topic
Don't think it is supported yet. Take a look at this JIRA tracking Delete Topic. For delete manually ...
- 页面载入时通过获取GridView某行某列的值来控制某一列的控件属性
通过获取状态来控制"查看"button的Visible属性值. 在前台GridView中加入 OnRowDataBound="GridView1_RowDataBound ...
- 基于jquery的锚点滚动插件(百度百科效果) anchorScroll.js
1.插进使用场景 请打开https://baike.baidu.com/item/%E6%97%A5%E6%9C%AC%E5%8A%A8%E7%94%BB#hotspotmining,查看百度百科页面 ...
- jqGrid删除多行数据问题
var consoleDlg = $("#delcostListDlg"); var selectedRowIds = $("#costList").jqGri ...
- eclipse to avoid the message, disable the...
标题 CreateTime--2018年5月9日10:38:15 Author:Marydon 1.问题描述 2.问题解析 这是因为eclipse的智能提示超时引起的,将超时间调大即可,如:200 ...
- newInstance() 的参数版本与无参数版本
通过反射创建新的类示例,有两种方式: Class.newInstance() Constructor.newInstance() 以下对两种调用方式给以比较说明: Class.newInstance ...
- ASP.NET#使用母版时,如果要使用js中的getElementById()方法取得某个内容页的元素时要注意的问题
当使用母版,要使用js中的getElementById()方法取得某个内容页的元素时,所选取的id并不是母版中内容页的id,而是在设计内容页时设定的id例子:母版页: ...... <head ...