How to cancel winform button click event?
Asked Answered
A

3

6

I have a custom button class inherited from System.Windows.Forms.Button.

I want to use this button in my winform project.

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.

Here is my class source.

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.

Ahner answered 22/7, 2014 at 23:56 Comment(1)
Why do you have to cancel the click? Just ignore it and do nothing. Or handle only if (res == System.Windows.Forms.DialogResult.Yes) `Hornstein
G
7

You need to override the click event.

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);
    }
}
Gimcrack answered 23/7, 2014 at 0:9 Comment(1)
Thanks for your answer, I didn't even try to look up override.Ahner
S
5

Here's another way to deal with this general kind of problem. (This is not meant as competition to the previous answer, but just food for thought.) Turn the dialogResult property of your button to none then handle it in the code. An OK button example here:

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();
}
Stomodaeum answered 31/5, 2016 at 23:22 Comment(0)
H
0

I guess you can use 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;

            }
        }
    }
}

like that

Handler answered 17/2, 2021 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.