Results 1 to 4 of 4

Thread: Call this.Hide() but the Form is still activated on Win10

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Call this.Hide() but the Form is still activated on Win10

    I have a dropdown Form to show some tips by clicking a button. The codes work on Win7 and XP for many years but it has problem on Win10.
    Code:
    private void MyDropDownForm_Deactivate(object sender, EventArgs e)
        {                       
            Console.WriteLine("Form.ActiveForm.Name_b4Hide = " + Form.ActiveForm.Name);
            Console.WriteLine("base.Focused_b4Hide = " + base.Focused);
            this.Hide();
            Console.WriteLine("base.Focused_a4Hide = " + base.Focused);
            Console.WriteLine("Form.ActiveForm.Name_a4Hide = " + Form.ActiveForm.Name);                 
        }
    On Win7, the debug window show:
    Code:
    Form.ActiveForm.Name_b4Hide = "MyDropDownForm"
    base.Focused_b4Hide = true
    base.Focused_a4Hide = false
    Form.ActiveForm.Name_a4Hide = "MainForm"
    But on Win10,the debug window show:
    Code:
     Form.ActiveForm.Name_b4Hide = "MyDropDownForm"
     base.Focused_b4Hide = true
     base.Focused_a4Hide = true
     Form.ActiveForm.Name_a4Hide = "MyDropDownForm"
    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.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    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?
    Attached Files Attached Files
    Last edited by DaveDavis; Jul 24th, 2020 at 10:38 AM.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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.

    Regards,

    - Aaron.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Call this.Hide() but the Form is still activated on Win10

    Quote Originally Posted by Aaron Young View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width