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

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

问题描述

我有一个 Windows 窗体应用程序.我在这个应用程序中有几个表单(一个主表单和几个专门的表单),并且只有一个表单,我的任何按钮都没有触发点击事件.

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.

如果您处理TextBox 等控件的Validating 事件并设置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:

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

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