如何在 .NET 单选按钮中获取 DoubleClick 事件? [英] How do I get a DoubleClick event in a .NET radio button?

查看:28
本文介绍了如何在 .NET 单选按钮中获取 DoubleClick 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从标准的 winforms 单选按钮捕获 DoubleClick 或 MouseDoubleClick 事件,但它们似乎被隐藏并且不起作用.目前我有这样的代码:

I'd like to be able to catch the DoubleClick or MouseDoubleClick events from a standard winforms radio button, but they seem to be hidden and not working. At the moment I have code like this:

public class RadioButtonWithDoubleClick : RadioButton
{
    public RadioButtonWithDoubleClick()
        : base()
    {
        this.SetStyle( ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true );
    }

    [EditorBrowsable( EditorBrowsableState.Always ), Browsable( true )]
    public new event MouseEventHandler MouseDoubleClick;
    protected override void OnMouseDoubleClick( MouseEventArgs e )
    {
        MouseEventHandler temp = MouseDoubleClick;
        if( temp != null ) {
            temp( this, e );
        }
    }
}

有没有更简单更干净的方法?

Is there a simpler and cleaner way to do it?

对于背景,我同意 Raymond Chen 的帖子 这里 双击单选按钮的能力(如果这些是对话框上的 only 控件)使对话框只是对于了解它的人来说,使用起来会更容易一些.

For background, I agree with Raymond Chen's post here that the ability to double click on a radio button (if those are the only controls on the dialog) makes the dialog just a tiny bit easier to use for people who know about it.

在 Vista 中使用任务对话框(请参阅 thisMicrosoft 指南页面此 MSDN 页面专门关于任务对话框 API) 将是显而易见的解决方案,但我们没有这样的奢侈.

In Vista using Task Dialogs (see this Microsoft guideline page or this MSDN page specifically about the Task Dialog API) would be the obvious solution, but we don't have the luxury of that.

推荐答案

根据您最初的建议,我提出了一个解决方案,无需使用反射对单选按钮进行子类化:

Based on your original suggestion I made a solution without the need to subclass the radiobuton using reflection:

MethodInfo m = typeof(RadioButton).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
if (m != null)
{
    m.Invoke(radioButton1, new object[] { ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true });
}
radioButton1.MouseDoubleClick += radioButton1_MouseDoubleClick;

现在触发了单选按钮的双击事件.顺便说一句:Nate 使用 e.Clicks 的建议不起作用.在我的测试中,无论我单击单选按钮的速度或频率如何,e.Clicks 始终为 1.

Now the double click event for the radiobutton is fired. BTW: The suggestion of Nate using e.Clicks doesn't work. In my tests e.Clicks was always 1 no matter how fast or often I clicked the radiobutton.

这篇关于如何在 .NET 单选按钮中获取 DoubleClick 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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