none
How do I setup Modal Forms to Always Display In Front of the Main Form (Form1) without Using TopMost Function? RRS feed

  • Question

  • Hello community,

    I Would really appreciate some help setting up my forms correctly on my current project if possible please...

    The issue I'm having is...
    Assuming that Form1 is my main form, or in other words the form I want to always be visible behind any other forms I show throughout my app. If I then show a second form on top of the main form, when I re-select Form1 this obviously sends the second form behind Form1.

    What I envisage for my app is that Form1 (the main form) would always be behind any other forms while still in front of other background application (as normal). While at the same time any other forms I display after form1 is shown, would always be in front of Form1, even if I re- select Form1, the second form will remain on top of Form1 but not on top of everything else.
    On saying 'everything else' I do realize I can set forms to topmost to keep them always on top of any others, however this tends to interfere with normal windows function forms and other forms i display. Then can unintentionally end up hidden by the topmost form. Even more so if the pc running the app only has a small display.

    I also realize showing the form as dialog will keep the form on top, but that also keeps it focused, meaning I cant return to Form1 without closing the dialog form.


    As a workaround I thought of setting up and event handler to trigger whenever Form1 gets focus or is activated (or an event similar) to then bring any forms with a normal window state forward with BringToFront(); could possibly have the desired effect.
    Although, if this was to be viable I am not quite sure of the correct event trigger to use for the task. I Have tried Enter, Focused, Activated events but they did not have the desired effect... Or the forms i bring to front are just getting sent straight back behind Fom1 again when the form gets focus, making the whole attempt redundant.

    So assuming I haven't confused the issue, my question would be...

    What is the best way to achieve this please? And, Is there a mainstream or accepted 'standard' way of doing this?

    Any suggestions on the correct way to set this up, or a link to relevant page would be greatly appreciated.

    Thank you, all replies are appreciated.

    {o_o}



    • Edited by AussieHack Tuesday, July 4, 2017 4:11 AM typo
    Tuesday, July 4, 2017 4:04 AM

Answers

  • What I envisage for my app is that Form1 (the main form) would always be behind any other forms while still in front of other background application (as normal). While at the same time any other forms I display after form1 is shown, would always be in front of Form1, even if I re- select Form1, the second form will remain on top of Form1 but not on top of everything else.

    This is the part that defines your task.   You are saying that form1 should become accessible when form 2 is selected, even though form 2 will become the active form.  That is, form2 is always to be associated with form 1 at the application level, without interfering with the normal interaction between different applications.

    That's what a MDI form is designed to do - associate the child forms with the parent as a single group. A MDI form cannot, however, exist outside the boundaries of the parent form.   The reasons for that are associated with the problems you have described in trying to implement this association using separate forms.  If you are prepared to accept that the child form is always contained within the parent, then MDI is the way to go.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/multiple-document-interface-mdi-applications

    • Marked as answer by AussieHack Friday, August 25, 2017 4:54 AM
    Tuesday, July 4, 2017 5:55 AM

All replies

  • If you use this method of displaying Form2 from Form1:

       Form2.Show

    Then change it to:

       Form2.Show(Me)

    The same technique can be used in case of New:

       Dim f2 As New Form2
       f2.Show(Me)


    Tuesday, July 4, 2017 5:00 AM
  • What I envisage for my app is that Form1 (the main form) would always be behind any other forms while still in front of other background application (as normal). While at the same time any other forms I display after form1 is shown, would always be in front of Form1, even if I re- select Form1, the second form will remain on top of Form1 but not on top of everything else.

    This is the part that defines your task.   You are saying that form1 should become accessible when form 2 is selected, even though form 2 will become the active form.  That is, form2 is always to be associated with form 1 at the application level, without interfering with the normal interaction between different applications.

    That's what a MDI form is designed to do - associate the child forms with the parent as a single group. A MDI form cannot, however, exist outside the boundaries of the parent form.   The reasons for that are associated with the problems you have described in trying to implement this association using separate forms.  If you are prepared to accept that the child form is always contained within the parent, then MDI is the way to go.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/multiple-document-interface-mdi-applications

    • Marked as answer by AussieHack Friday, August 25, 2017 4:54 AM
    Tuesday, July 4, 2017 5:55 AM
  • Hi AussieHack,

    It seems that Viorel_ solved your issue, please remember to close your thread by marking his post as answer, it is very beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    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, July 19, 2017 6:32 AM
    Moderator
  • thank you very much for replies.

    MDI Interface took a while to implement across many forms in my app project, but it does do exactly what I wanted. So TYVM yet again Acamar :)

    Sort of makes Form1 work like a desktop would (of sorts) with all other forms always on top while still being able to switch focus between them all...

    hmmm... I wish now I had used that explanation when I asked the question, instead of the confusing essay question I did post.

    • Edited by AussieHack Friday, August 25, 2017 5:07 AM
    Friday, August 25, 2017 4:57 AM