Click event does not fire - cannot change focus - cannot close form

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

It's not that the code in the handler does not work. This may be due to the fact that a breakpoint in the first line of the handler is never reached when a button is pressed.

Other events work (I use CheckedChanged events in this form and they behave).

My team members reviewed, and also cannot identify 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"); } } 

Change: additional information from testing

In my form, there are several data-related drop-down lists. I found that the click event does not fire only if I first make a selection from the drop-down list.

If I do not make a choice, the breakpoint in the button handler fires. Otherwise, it is not. There are no registered events in these drop-down lists.

+6
source share
3 answers

Here is the reason:

When using data binding, when you enter a value in a data-binding control, it first tries to validate the record, and then if the record was valid, the data binding will put the value in the data source, but if a validation error occurs, the verification will return false and your control enters into invalid mode.

If the child form control is not validated, by default you cannot change focus from an invalid control.

Clicking on a button by default causes a control to be checked, which loses focus, so you cannot click on the button, because you see that your button reflects the mouse, but does not actually click.

The same problem will occur if you handle the Validating event of a control such as a TextBox and set e.cancel = true .

Here is the fix:

You can fix this behavior using one of the following options:

+8
source

I found the problem after further testing.

I have a problem not with events with buttons, but with a form lock after selecting from the drop-down list.

I have not yet discovered why the form locks after the drop-down list are selected (it has no events, but there is a binding to them, so there are some possible reasons).

Thank you for your help!

+1
source

It will help you

Edit

 public ScheduleMeeting() { InitializeComponent(); } 

to

 public MyForm() { InitializeComponent(); } 
0
source

Source: https://habr.com/ru/post/1233830/


All Articles