【问题标题】:How to cancel winform button click event?如何取消winform按钮点击事件?
【发布时间】: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)`

标签: c# button onclick click


【解决方案1】:

这是处理此类一般问题的另一种方法。 (这并不意味着与上一个答案竞争,而只是为了思考。)将按钮的 dialogResult 属性设置为 none,然后在代码中处理它。这里有一个 OK 按钮示例:

private void OKUltraButton_Click(object sender, Eventargs e)
{
    {
    //Check for the problem here, if true then...
        return;
    }

    //Set Dialog Result and manually close the form
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.Close();
}

【讨论】:

    【解决方案2】:

    您需要覆盖点击事件。

    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);
        }
    }
    

    【讨论】:

    • 感谢您的回答,我什至没有尝试查找覆盖。
    【解决方案3】:

    我猜你可以使用return

    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)
                {
                    return;
    
                }
            }
        }
    }
    

    这样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2018-07-08
      • 2015-08-28
      相关资源
      最近更新 更多