Let's say I have a winforms application with two forms, the main form that starts when the program starts, and another form. Here is the code for the main form:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var f2 = new Form2(); f2.ShowDialog(); } private void textBox1_Validated(object sender, EventArgs e) { System.Diagnostics.Debug.Print("Main Form: Validated!"); } }
And this is the child form:
public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void textBox1_Validated(object sender, EventArgs e) { System.Diagnostics.Debug.Print("Child Form: Validated!"); } }
When I launch the application, I can put the focus in the text box in the main form, and of course, when I go to the tab, it fires the Validated event and prints Main Form: Validated! at the exit. This also happens if I have focus in the text box and closing the main form (i.e. terminating the program).
When I click on the button in the main form that issues an instance of the child form, I can put the focus in the text box on the child form, and the Validated event fires, as it should when I exit This. However, unlike the behavior of the main form when closing the form, if I have focus in the text box on the child form and I close the child form, the Validated event never fires.
Why the activated fire of events does not work, and is there a way I can launch it.
I rely on the checked event of certain controls to update my view models. I want to make sure that they always work, even when the loss of focus is associated with closing the form or even ending the application itself.