John Smith
John Smith

Reputation: 4516

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?

Upvotes: 6

Views: 8205

Answers (3)

JClaspill
JClaspill

Reputation: 1745

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?

Upvotes: 8

Sabri Ghaith
Sabri Ghaith

Reputation: 149

Try to wire below,

private void frmMyForm_Deactivate(object sender, EventArgs e)
    {
        // Raise your flag here.
    }

By wiring above event, it will tell you whenever the form is minimized, partially/totally hided by another form.

Upvotes: 2

Mark Segal
Mark Segal

Reputation: 5550

Well, as you know, windows might or might not have Focus. If a windows is focused, it is therefore that the user have clicked it. You can try the Focused property. Otherwise, I don't think there is a property which tells you if another window is above yours.
You can "give" focus, therefore "popping out" the window with the Focus() method.

Note: A window can be both focused and be under another window.
To determine if your window isn't under another window, I'm afraid you must go deeper.

Upvotes: -1

Related Questions