Search code examples
c#.netwinforms.net-2.0form-control

How do you tell if a Windows Form is open but behind another window?


Calling Form.Visible will return true regardless of whether the form is maximized, minimized, or has a FormWindowState of Normal.

What I want to know is how to tell if the form is open but "hidden" behind another application's window.

If that's the case, I want to bring it to the front and actually make it visible to the user.

I tried the BringToFront() method but it didn't work. I also tried calling the Show() method but if the form is behind another application's window, it remains that way.

The only workaround I found to the problem is setting the Form's FormWindowState to Minimized/Maximized and then normal, but that's a bit of a hack and doesn't look nice.

Can someone tell me how to tell if the form is behind another window and how to bring it to the front?


Solution

  • Strange.

    this.Activate() should do the trick.

    You can always try a 'horrible hack method', which I feel guilty for spreading. But if this.Activate() doesn't work, for the sake of testing you might try:

    this.TopMost = true;
    this.Focus();
    this.BringToFront();
    this.TopMost = false;
    

    I've never seen this recommended as a solution, but it might work to show you the functionality. I'd be more concerned about why this.Activate() isn't working if the above-mentioned code does.

    As for detecting the window, you cannot use a command to detect it via C# like that. Check the following questions answers for more info: How to check if window is really visible in Windows Forms?