none
How to Display Parent and Child Forms When the Parent is Re-Selected RRS feed

  • Question

  • My Winforms app has a non-modal Child Form.  I would like both to display, with the Child on top, when the user clicks on another app and the clicks back on the Main Form.  I have been playing with various things in the Main Form's Activated event handler, but so far no luck.  The best I can do is to make the Child the top Form is various ways, but the Child keeps focus since the Main event handler keeps giving it back each time I click on the Main Form.  Seems like there should be a simple fix without the monkey business of setting and checking various state variables/etc, but I'm stymied.

    Thanks, Steve.


    • Edited by Cincy Steve Tuesday, April 16, 2019 6:47 PM
    Tuesday, April 16, 2019 6:45 PM

Answers

  • If you create the child window with Form1 as owner, it will be on top

    Like :

     Form2 myForm2 = new Form2();
     myForm2.Text = "myForm2";
     myForm2.Owner = this;
     myForm2.Show();
    

    • Marked as answer by Cincy Steve Wednesday, April 17, 2019 5:52 PM
    Wednesday, April 17, 2019 1:53 PM

All replies

  • Hi Cincy,

    >> with the Child on top, when the user clicks on another app and the clicks back on the Main Form.

    Sorry, I could not fully understand your needs. Do you mean you want to always set the Child to top but the Parent get the focus? To handle the issue more efficiently, it would be better if you can provide more useful information.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 17, 2019 2:24 AM
    Moderator
  • Kyle -

    As you say, I want the Child on top but with the Parent having focus.  I want this to happen whenever the user clicks on some other app and then clicks back on my Parent app window.

    Thanks,

    Steve

    Wednesday, April 17, 2019 1:27 PM
  • If you create the child window with Form1 as owner, it will be on top

    Like :

     Form2 myForm2 = new Form2();
     myForm2.Text = "myForm2";
     myForm2.Owner = this;
     myForm2.Show();
    

    • Marked as answer by Cincy Steve Wednesday, April 17, 2019 5:52 PM
    Wednesday, April 17, 2019 1:53 PM
  • My code matches what you suggest for when I instantiate the Child Form, so it works as desired and expected at that point. 

    I am asking about how to re-display the Child Form (without recreating it) whenever the user clicks off my app (i.e. clicks on another app, moving it on top) and subsequently clicks back on my app in the tray at the bottom of the screen.  Without the code I am seeking, clicking on my app in the tray makes the Parent Form visible (i.e. moves it to the top), but the Child Form remains hidden.  Making both visible requires that I click on both the Parent and Child Form windows in the bottom tray.  I hope this is clear.

    Thanks,

    Steve

    Wednesday, April 17, 2019 2:16 PM
  • I cannot reproduce it: when I switch to another app and then click on the main form (Form1), the Form2 is displayed (Windows 10, VS 2015)
    Wednesday, April 17, 2019 2:21 PM
  • THANKS!  On closer examination, I missed the assignment of the Owner property.  I guess I assumed the control that instantiated myForm2 would have been its owner by default.  I thought there had to be a straightforward explanation, since there often is.

    Thanks again,

    Steve

    Wednesday, April 17, 2019 5:51 PM
  • THANKS!  On closer examination, I missed the assignment of the Owner property.  I guess I assumed the control that instantiated myForm2 would have been its owner by default.  I thought there had to be a straightforward explanation, since there often is.

    Thanks again,

    Steve

    Wednesday, April 17, 2019 5:51 PM