简体   繁体   中英

How to bring “all forms” to foreground (WindowsMobile/C#)

(C# / WindowsMobile 6)

Let's take an application with 3 STATIC forms: Form1, Form2, Form3, where Form1 opens Form2 by calling Form2.Show(), and Form2 does the same with Form3. Form2 and Form3 have a "Exit" button, that just hides the form (not "close", just hide).

So, we execute these steps:

  • open the application;
  • go to Form2, by clicking "Form2" button on Form1;
  • go to Form3, by clicking "Form3" button on Form2;
  • open File Explorer, and "re-open" application by clicking on it's file. Form3 appears;
  • hide Form3 by clicking on "Exit" button on Form3 ( this.Hide() ). That's the problem: file explorer appears instead Form2.

I don't want to call "callingform".Show() every time I hide a form. This "works", but file explorer screen appears after "this.Hide()" and before "callinform.Show()" and I need to "control" who's calling who.

How to solve this? Is there any way to bring all application's form to foreground in the same order they appeared?

Thanks in advance.

You might need to do some investigation into this, but off the top of my head you could try looking at the Application.Forms[] collection.

Maybe someone can confirm or deny this but I think usually, Application.OpenForms[0] will be the main/initial form with subsequent Form appearances being in Application.OpenForms[1], Application.OpenForms[2], etc...

So you could simply try navigating backwards through this Forms collections.

Something like (or a variation of),

public void BringLastOpenedFormToFront()
{
    if(Application.OpenForms.Count > 0)
    {
       Form form = Application.OpenForms[Application.OpenForms.Count - 1];
       BringToFront(form);  // your bring to front method.
    }
}

This would allow you to ensure the last Form that appeared was brought to the front and immediately visible to the user. Let me know if you need any clarification.

Link to MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx

There really isn't a way. You could implement a way to store forms in a similar way to the first answer, but when you switch you need to do:

"callingform".BringToFront();
"callingform".Show();

That will put all of your forms in front of Explorer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM