問(wèn)題描述
我有一個(gè)繼承自 System.Windows.Forms.Button 的自定義按鈕類.
I have a custom button class inherited from System.Windows.Forms.Button.
我想在我的 winform 項(xiàng)目中使用這個(gè)按鈕.
I want to use this button in my winform project.
這個(gè)類叫做ConfirmButton",它用Yes或No顯示確認(rèn)信息.
This class is called "ConfirmButton", and it shows confirm message with Yes or No.
但問(wèn)題是當(dāng)用戶選擇否并確認(rèn)消息時(shí),我不知道如何停止點(diǎn)擊事件.
But the problem is that I do not know how to stop click event when user selected No with confirm message.
這是我的課程來(lái)源.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace ConfirmControlTest
{
public partial class ConfirmButton : System.Windows.Forms.Button
{
public Button()
{
InitializeComponent();
this.Click += Button_Click;
}
void Button_Click(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("Would you like to run the command?"
, "Confirm"
, MessageBoxButtons.YesNo
);
if (res == System.Windows.Forms.DialogResult.No)
{
// I have to cancel button click event here
}
}
}
}
如果用戶從確認(rèn)消息中選擇否,則按鈕單擊事件不應(yīng)再觸發(fā).
If user select No from confirm message, then the button click event should not fire anymore.
推薦答案
你需要重寫(xiě)點(diǎn)擊事件.
class ConfirmButton:Button
{
public ConfirmButton()
{
}
protected override void OnClick(EventArgs e)
{
DialogResult res = MessageBox.Show("Would you like to run the command?", "Confirm", MessageBoxButtons.YesNo
);
if (res == System.Windows.Forms.DialogResult.No)
{
return;
}
base.OnClick(e);
}
}
這篇關(guān)于如何取消winform按鈕點(diǎn)擊事件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!