Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 962
Jamie Butler Avatar asked Oct 16 '15 03:10

Jamie Butler


3 Answers

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:

  • Set CausesValidation property of your button to false
  • Set AutoValidate property of your form to AutoValidate.EnableAllowFocusChange
like image 97
Reza Aghaei Avatar answered Nov 06 '22 02:11

Reza Aghaei


This will do the trick for you

Change

public ScheduleMeeting()
{
    InitializeComponent();
} 

to

public MyForm()
{
    InitializeComponent();
}
like image 37
Shweta Pathak Avatar answered Nov 06 '22 02:11

Shweta Pathak


I have discovered the issue after further testing.

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

I have not yet discovered why the form blocks after the drop down is selected (it has no events, but does have databinding, so there are some possible causes there).

Thank you for all your help!

like image 1
Jamie Butler Avatar answered Nov 06 '22 02:11

Jamie Butler