How to cancel winform button click event?(如何取消winform按钮点击事件?)
问题描述
我有一个继承自 System.Windows.Forms.Button 的自定义按钮类.
I have a custom button class inherited from System.Windows.Forms.Button.
我想在我的 winform 项目中使用这个按钮.
I want to use this button in my winform project.
这个类叫做ConfirmButton",它用Yes或No显示确认信息.
This class is called "ConfirmButton", and it shows confirm message with Yes or No.
但问题是当用户选择否并确认消息时,我不知道如何停止点击事件.
But the problem is that I do not know how to stop click event when user selected No with confirm message.
这是我的课程来源.
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 user select No from confirm message, then the button click event should not fire anymore.
推荐答案
你需要重写点击事件.
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);
}
}
这篇关于如何取消winform按钮点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何取消winform按钮点击事件?
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
