简体   繁体   English

C#中的按钮和事件

[英]Buttons and Events in c#

How come when you do button1.Enabled = false; 当您执行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); 我想禁用该按钮,以便在按下按钮时不执行button1就不会触发.Click事件。Click-= new EventHandler(panel_Click);

What should i 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. 在ASP.Net和Win Forms中都执行了测试之后,必须简单地就是流氓代码调用此方法。 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 到达后,单击菜单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. 它不应触发Click事件。

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. 上面将添加一个禁用到您的窗体的按钮,并且MessageBox将不会显示。 But if you set Enabled to true, it will. 但是,如果将Enabled设置为true,它会。

I noticed that you are attempting to remove the panel_Click handler from the button's click event. 我注意到您正在尝试从按钮的 click事件中删除panel_Click处理程序。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM