Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

c# - Click Event Not Firing - Cannot Change Focus - Cannot Close Form

I have a Windows Forms Application. I have several forms in this application (a main form, and several specialized forms), and on only one form, click events are not firing for any of my buttons.

It is not that the code in the handler is broken. This can be determined by the fact that a breakpoint on the first line of the handler is never reached when clicking the button.

Other events are working (I'm using CheckedChanged events on this form and they are behaving).

My team members have reviewed, and also can't spot the problem.

Here is a simplified view of my code:

Designer Generated Code

partial class MyForm
{
    private System.Windows.Forms.Button addButton;

    private void InitalizeComponent()
    {
        this.addButton = new System.Windows.Forms.Button();
        this.addButton.Name = "addButton";
        // Drawing statements here
        this.addButton.Click += new System.EventHandler(this.addButton_Click);

        this.Controls.Add(this.addButton);
    }
}

My Code

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
    }

    private void addButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("The debugger is not reaching a break point on this line");
    }
}

Edit: Additional Information from Testing

There are several data-bound dropdownlists in my form. I have discovered that the click event only fails to fire if I make a selection in a drop down box first.

If I make no selections, the break point in the button's handler fires. Otherwise it doesn't. There are no events registered on these drop down lists.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the reason:

When using data binding, when you enter a value in a data bound control, it first tries to validate entry and then if the entry was valid, data binding will put the value in data source, but if a validation error occurs validation returns false and your control goes to invalid mode.

When a child control of form didn't validate, by default you can not change focus from invalid control.

Click on a button by default causes validation of the control that are losing the focus, so you can't click on button, as you see your button reflect to mouse but not actually click.

The same problem will happen if you handle Validating event of a control like TextBox and set e.cancel = true.

Here is the fix:

you can fix this behavior using either of following options:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...