开发者

Buttons and Events in c#

开发者 https://www.devze.com 2023-02-06 01:56 出处:网络
How come when you do button1.Enabled = false; if you still click it the Event Handler Click that was added to earlier will still trigger it ?

How come when you do button1.Enabled = false; if you still click it the Event Handler Click that was added to earlier will still trigger it ?

I want to disable the button so when pressing it won't trigger the .Click event without doing button1.Click -= new EventHandler(panel_Click);

What should i 开发者_C百科do?


I have never tested if the event will still trigger. Seems counterintuitive that it would. You could simply overcome this by placing the following statement in your callback method

if (!button1.Enabled)
{
   return;
}

This will break out of your method so any subsequent code will not execute.

Edit:

After performing tests in both ASP.Net and Win Forms, it must simply be that you have a rogue line of code calling this method. Easy mistake to make, and very hard sometimes to track down. However, utlizing the Call Stack Window makes this cake.

Simply place a break point in your callback method. When it's reached click on the menu Debug -> Windows -> Call Stack

This will show you where the method was invoked from. Double click on any given item in the window and it will redirect you to the method where the invocation occurs. You will find your bug using this.


It shouldn't trigger the Click Event.

public MyForm()
{
    InitializeComponent();

    var button = new Button {Enabled = false};

    button.Click += ButtonClick;

    Controls.Add(button);
}

void ButtonClick(object sender, EventArgs e)
{
    MessageBox.Show(@"Clicked!");
}

The above will add a button that is disabled to your form, and the MessageBox will not show. But if you set Enabled to true, it will.


I noticed that you are attempting to remove the panel_Click handler from the button's click event. Are you sure the click event for the button is being fired? Does the panel that the button is added to have a click handler? Even if the button is disabled, any other controls behind it that have applicable events will still fire.

Here's a small demonstration:

public class TestForm : Form
{
    public TestForm()
    {
        this.Text = "Test Form";

        var panel = new FlowLayoutPanel
        {
            FlowDirection = FlowDirection.TopDown,
            BorderStyle = BorderStyle.FixedSingle,
        };
        var button = new Button
        {
            Text = "Button!",
            Enabled = false,
        };
        var cb = new CheckBox
        {
            Text = "Buton Enabled",
            Checked = false,
        };

        panel.Click += (sender, e) => MessageBox.Show("Panel clicked!");
        button.Click += (sender, e) => MessageBox.Show("Button clicked!");
        this.Click += (sender, e) => MessageBox.Show("Form clicked!");
        cb.CheckedChanged += (sender, e) => button.Enabled = cb.Checked;

        panel.Controls.Add(button);
        panel.Controls.Add(cb);
        this.Controls.Add(panel);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消