Why this.Hide() produces different results on win10 vs win7? How to force the hiden form to de-activate the MyDropDownForm after calling this.Hide() on win10?
Last edited by DaveDavis; Jul 24th, 2020 at 10:39 AM.
Re: Call this.Hide() but the Form is still activated on Win10
It is just a simple demo by clicking anywhere on MainForm to call DropDownForm_Deactivate, don't click anywhere on desktop to avoid an error . Are you seeing different results on Win7 and Win10?
Last edited by DaveDavis; Jul 24th, 2020 at 10:38 AM.
Re: Call this.Hide() but the Form is still activated on Win10
I believe your call to Hide within the Form's Deactivate event is disrupting the Event chain which results in the Main Form remaining inactive.
You could start a secondary thread and make your Hide call from there (using the Invoke method because you need it to return to and execute on the UI thread.)
This will allow the normal Deactivate event chain to complete (making the Main Form active) and execute your additional code to Hide the popup form afterward, ex.
Code:
private void DropDownForm_Deactivate(object sender, EventArgs e) //Occurs when the form loses focus and is no longer the active form.
{
Console.WriteLine("Form.ActiveForm.Name_b4Hide = " + Form.ActiveForm.Name);
Console.WriteLine("base.Focused_b4Hide = " + base.Focused);
Task.Run(() => this.Invoke((MethodInvoker)(() => {
base.Hide();
Console.WriteLine("base.Focused_a4Hide = " + base.Focused);
Console.WriteLine("Form.ActiveForm.Name_a4Hide = " + Form.ActiveForm.Name);
})));
}
I noticed your project is targeting .NET Framework 2.0 which doesn't support the Task library, in which case you can achieve the same result by setting up a simple Timer component on the Popup form which is enabled in the Deactivate event. You would then execute your delayed code in the Timer event after disabling the Timer.
Re: Call this.Hide() but the Form is still activated on Win10
Originally Posted by Aaron Young
I believe your call to Hide within the Form's Deactivate event is disrupting the Event chain which results in the Main Form remaining inactive.
You could start a secondary thread and make your Hide call from there (using the Invoke method because you need it to return to and execute on the UI thread.)
This will allow the normal Deactivate event chain to complete (making the Main Form active) and execute your additional code to Hide the popup form afterward, ex.
Code:
private void DropDownForm_Deactivate(object sender, EventArgs e) //Occurs when the form loses focus and is no longer the active form.
{
Console.WriteLine("Form.ActiveForm.Name_b4Hide = " + Form.ActiveForm.Name);
Console.WriteLine("base.Focused_b4Hide = " + base.Focused);
Task.Run(() => this.Invoke((MethodInvoker)(() => {
base.Hide();
Console.WriteLine("base.Focused_a4Hide = " + base.Focused);
Console.WriteLine("Form.ActiveForm.Name_a4Hide = " + Form.ActiveForm.Name);
})));
}
I noticed your project is targeting .NET Framework 2.0 which doesn't support the Task library, in which case you can achieve the same result by setting up a simple Timer component on the Popup form which is enabled in the Deactivate event. You would then execute your delayed code in the Timer event after disabling the Timer.
Regards,
- Aaron.
I haven't tested but you may not need the Task.Run and just call BeginInvoke instead.