WinForms 如何在按钮上调用双击事件? [英] WinForms how to call a Double-Click Event on a Button?

查看:25
本文介绍了WinForms 如何在按钮上调用双击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望该事件仅在双击按钮时发生,而不是在单击一次按钮时发生.遗憾的是,双击事件没有出现在 IDE 的事件列表中.

Rather than making an event occur when a button is clicked once, I would like the event to occur only when the button is double-clicked. Sadly the double-click event doesn't appear in the list of Events in the IDE.

有人知道解决这个问题的好方法吗?谢谢!

Anyone know a good solution to this problem? Thank you!

推荐答案

否,标准按钮对双击没有反应.请参阅 Button.DoubleClick 事件的文档.它不会对双击做出反应,因为在 Windows 中按钮总是对点击做出反应,而从不对双击做出反应.

No the standard button does not react to double clicks. See the documentation for the Button.DoubleClick event. It doesn't react to double clicks because in Windows buttons always react to clicks and never to double clicks.

你讨厌你的用户吗?因为您将创建一个与系统中任何其他按钮行为不同的按钮,而有些用户永远不会弄明白这一点.

Do you hate your users? Because you'll be creating a button that acts differently than any other button in the system and some users will never figure that out.

也就是说,您必须创建一个从 Button 派生的单独控件来触发此事件(因为 SetStyle 是一个受保护的方法)

That said you have to create a separate control derived from Button to event to this (because SetStyle is a protected method)

public class DoubleClickButton : Button
{
    public DoubleClickButton()
    {
        SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
    }
}

然后您必须在代码中手动添加 DoubleClick 事件处理程序,因为它仍然不会出现在 IDE 中:

Then you'll have to add the DoubleClick event hanlder manually in your code since it still won't show up in the IDE:

public partial class Form1 : Form {
    public Form1()  {
        InitializeComponent();
        doubleClickButton1.DoubleClick += new EventHandler(doubleClickButton1_DoubleClick);
    }

    void doubleClickButton1_DoubleClick(object sender, EventArgs e)  {
    }
}

这篇关于WinForms 如何在按钮上调用双击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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