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 事件?
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01