的WinForms Click事件不触发 [英] Winforms Click Event Not Firing

查看:152
本文介绍了的WinForms Click事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winforms应用程序。我在这个应用程序中有几种形式(主要形式和几种专用形式),而且只有一种形式,点击事件不会触发我的任何按钮。



这不是处理程序中的代码坏了。这可以通过在点击按钮时从未到达处理程序的第一行上的断点的事实来确定。



其他事件正在工作(我使用CheckedChanged



我的团队成员已经审核,也无法发现问题。



以下是我的代码的简化视图:



设计器生成代码


$ b b

 部分类MyForm 
{
private System.Windows.Forms.Button addButton;

private void InitalizeComponent()
{
this.addButton = new System.Windows.Forms.Button();
this.addButton.Name =addButton;
//这里绘制语句
this.addButton.Click + = new System.EventHandler(this.addButton_Click);

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

我的代码 p>

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

private void addButton_Click(object sender,EventArgs e)
{
MessageBox.Show(调试器未到达此行上的断点) ;
}
}

编辑: strong>



我的表单中有几个下拉列表。我发现点击事件只有在我首先在下拉框中进行选择时才会触发。



如果我没有选择,按钮的断点处理程序触发。否则不会。

解决方案

正如您在帖子中提到的:


问题不在于按钮事件,而是在从下拉框中选择后阻止
的形式。


这是原因:



使用数据绑定时,在数据绑定控件中输入一个值,它首先尝试验证条目,然后如果条目有效,数据绑定将把值放在数据源中,但如果验证错误发生,验证返回false,您的控制转到无效模式。



当窗体的子控件未验证时,默认情况下您不能将焦点从无效控件更改。



默认情况下点击按钮会导致失去焦点的控件的验证,因此您不能点击按钮,因为您看到您的按钮反映到鼠标, TextBox 并设置 e.cancel = true



修正:



您可以使用以下选项修正此问题:




  • CausesValidation 属性设置为 false

  • 设置 AutoValidate 您的表单的属性 AutoValidate.EnableAllowFocusChange


I have a winforms application. I have several forms in this application (a main form, and several specialised 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 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.

解决方案

As you mentioned in your post:

The issue is not with button events, but with the form becoming blocked after making a selection from a drop down box.

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 losting the focus, so you can't click on button, as you see your button reflect to mouse but not actually click.

You can simply simulate such behavior by handling Validating event of a TextBox and set e.cancel = true.

Here is the fix:

you can fix this behavior using this options:

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

这篇关于的WinForms Click事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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