单击事件未触发-无法更改焦点-无法关闭表单 [英] Click Event Not Firing - Cannot Change Focus - Cannot Close Form

查看:104
本文介绍了单击事件未触发-无法更改焦点-无法关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Forms应用程序。在此应用程序中,我有几种形式(一个主形式和几种专门形式),并且只有一种形式不会为我的任何按钮触发点击事件。

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.

其他事件正在起作用(我正在使用 CheckedChanged 在此表单上的事件,并且它们的行为是正常的。)

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:

设计师生成的代码

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

我的代码

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

编辑:来自测试的其他信息

我的表单中有几个数据绑定下拉列表。我发现只有首先在下拉框中进行选择时,单击事件才会失败。

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.

推荐答案

这是原因:

使用数据绑定时,在数据绑定控件中输入值时,它将首先尝试验证条目,然后如果该条目有效,则数据绑定会将值放入数据源,但是如果发生验证错误,则验证返回false,并且您的控件进入无效模式。

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.

如果您处理<$ c $之类的控件的 Validating 事件,也会发生同样的问题。 c> TextBox 并设置 e.cancel = true

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

此处解决方法是

您可以使用以下任一选项解决此问题:

you can fix this behavior using either of following options:

  • Set CausesValidation property of your button to false
  • Set AutoValidate property of your form to AutoValidate.EnableAllowFocusChange

这篇关于单击事件未触发-无法更改焦点-无法关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆