【发布时间】:2014-09-14 00:03:36
【问题描述】:
我有一个继承自 System.Windows.Forms.Button 的自定义按钮类。
我想在我的 winform 项目中使用这个按钮。
这个类叫做“ConfirmButton”,它用Yes或No显示确认信息。
但问题是当用户选择否并确认消息时,我不知道如何停止点击事件。
这是我的课程来源。
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
}
}
}
}
如果用户从确认消息中选择否,则按钮单击事件不应再触发。
【问题讨论】:
-
为什么必须取消点击?只是忽略它,什么也不做。或者只处理
if (res == System.Windows.Forms.DialogResult.Yes)`