WinForms TreeView检查事件丢失 [英] WinForms TreeView Check Events Being Lost

查看:28
本文介绍了WinForms TreeView检查事件丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我双击WinForms TreeView 中未选中的节点时,它会在外观上变为选中状态,然后变为未选中状态.

When I double-click an unchecked node in a WinForms TreeView, it visually goes to checked followed by unchecked.

但是,由以下事件处理程序检测到的仅触发了一个 AfterCheck 事件:

However only one AfterCheck event is fired as detected by the following event handler:

private void treeView1_AfterCheck( object sender, TreeViewEventArgs e )
{
    System.Diagnostics.Debug.WriteLine( "{0} {1}: {2}", e.Node.Checked, e.Node, e.Action );
}

即使未选中GUI中的视觉表示,

e.Node.Checked true .

e.Node.Checked is true even though the visual representation in the GUI is unchecked.

再次单击该复选框将引发一个 AfterCheck 事件,其中 Node.Checked 等于 false .该复选框在GUI上保持未选中状态.

Clicking the checkbox again raises an AfterCheck event with Node.Checked equal to false. The checkbox remains unchecked on the GUI.

反之亦然,双击已检查的节点.

Vice-versa for double-clicking a checked node.

我正在使用Visual Studio 2010为.NET 4.0进行编译,并在.NET 4.5.1上运行.

I'm compiling for .NET 4.0 with Visual Studio 2010 and running on .NET 4.5.1.

有什么解决方法吗?

推荐答案

当您双击树视图中的复选框时,不仅会在检查后事件中造成问题,还会在您的下一个鼠标单击时产生问题,如果单击在树状视图本身的其他任何位置,您的下一次鼠标单击都将丢失.

When you double click on check boxes in treeview, not only it makes problem in after check event, also it makes problem in your next mouse down and if you click anywhere else that the treeview itself, your next mouse click will be lost.

Hans Passant在旧帖子中也提到过: Vista版本的本机Windows控件在TreeView包装器中引起了一个错误.它会自动检查项目,而不会生成包装程序可以检测到引发前检查和后检查事件的通知消息.

Also mentioned by Hans Passant in an old post : The Vista version of the native Windows control induced a bug in the TreeView wrapper. It automatically checks an item without generating a notification message that the wrapper can detect to raise the BeforeCheck and AfterCheck event.

我可以确认该问题在Windows XP中不存在,但在7和8.1中存在.

I can confirm that the problem doesn't exists in Windows XP, but exists in 7 and 8.1.

要解决此问题,您可以处理 WM_LBUTTONDBLCLK 消息并检查是否双击复选框,而忽略它:

To solve the problem, you can hanlde WM_LBUTTONDBLCLK message and check if double click is on check box, neglect it:

public class ExTreeView : TreeView
{
    private const int WM_LBUTTONDBLCLK = 0x0203;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDBLCLK)
        {
            var info = this.HitTest(PointToClient(Cursor.Position));
            if (info.Location == TreeViewHitTestLocations.StateImage)
            {
                m.Result = IntPtr.Zero;
                return;
            }
        }
        base.WndProc(ref m);
    }
}

这篇关于WinForms TreeView检查事件丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆