How do I get a DoubleClick event in a .NET radio button?(如何在 .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 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 .NET 单选按钮中获取 DoubleClick 事件?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01