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 事件?
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
