10

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

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 );
        }
    }
}

有没有更简单、更干净的方法来做到这一点?

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

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

4

4 回答 4

11

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;

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.

于 2010-09-03T13:55:26.560 回答
3

你可以这样做:

myRadioButton.MouseClick += new MouseEventHandler(myRadioButton_MouseClick);

void myRadioButton_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Clicks == 2)
    {
         // Do something
    }
}

您可能也可能不想检查 e.Button == MouseButtons.Left

于 2009-06-30T17:34:42.993 回答
1

基于@MSW的回答,我做了这个扩展类:

static class RadioButtonEx
{
    public static void AllowDoubleClick(this RadioButton rb, MouseEventHandler MouseDoubleClick)
    {
        //
        // Allow double clicking of radios
        System.Reflection.MethodInfo m = typeof(RadioButton).GetMethod("SetStyle", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (m != null)
            m.Invoke(rb, new object[] { ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true });

        rb.MouseDoubleClick += MouseDoubleClick;
    }
}

然后超级容易设置和重复使用:

radioButton.AllowDoubleClick((a, b) => myDoubleClickAction());
于 2016-01-28T09:33:05.443 回答
0

对不起,没有声誉对此发表评论。您试图让用户双击执行什么操作?我认为使用双击可能会令人困惑,因为它与用户对单选按钮的一般心理模型不同(IE 单击,从一组中选择一个选项)

于 2009-07-01T03:09:39.107 回答