How to keep a window on top of all other windows in my application only?
Asked Answered
K

2

3

I would like to display a status window in my C# Windows Forms application that informs the user when the application is waiting to acquire a lock. This is an application-defined thing, however, the window should be visible and always remain on top of all other windows of my application, even when the user clicks on another window (like for example the larger main window behind it).

It must not be modal (so ShowDialog() cannot be used) because the app needs to keep trying in the background and close the window automatically if the lock could eventually be acquired, and it really should not be top-most for the entire window station (i.e. all applications running in that terminal session).

I know the Form.TopMost property, but it can only bring and keep a single window above all others, even those from other applications. This is clearly not what I'm looking for.

I know that this is possible, I've seen it many times before in other applications. I just don't know how it can be done.

Kurtzman answered 8/9, 2009 at 10:42 Comment(1)
Not directly related but if you want to launch multiple different form classes on top of the main parent and maintain the original opening timeline of events then omit the owner property altogether. This way each child window opened by the parent will stay opened on top of the parent. Only hidden when interacting with the parent window but will remain activated but lost focus when interacting with other child windows opened by the parent. Inconsistent use of the owner property results in weird and erratic behavior on child windows.Shanon
J
9

If you pass your main form into the Show method of the status form, it will stay on top of the main form, but not on top of other applications. So, in the main form you can have code like so:

StatusForm statusForm = new StatusForm();
statusForm.Show(this);

However, this will only point out one single window of your application as the owner.

Juniorjuniority answered 8/9, 2009 at 10:46 Comment(1)
It also seems to work with multiple windows. (At least multiple instances of the same Form class.) All of them remain in front of the owning window.Kurtzman
F
4

You have to set the Owner property of the child form to the parent form, and use Show to show the child form.

Flophouse answered 8/9, 2009 at 10:47 Comment(1)
Thank you, both solutions work just fine. Unfortuinately I can only mark one answer as accepted solution (but up-vote both). I also found out that it is good to set the ShowInTaskbar property of the owned windows to false so that they won't mess up the taskbar and Alt+Tab window list.Kurtzman

© 2022 - 2024 — McMap. All rights reserved.