C# GUI - Make a child window stay on top at all times

Continuing on from my previous work this week (in using group boxes and finding UI controls by name), this post looks at how we can force a child window (such as a pop-up) to stay on top of the main window at all times.

This can be particularly useful when you want the new window to stay present for the user to deal with, rather than becoming lost behind other windows that may be open in the way.

Setting the owner

When you open a new window programmatically, you can set who its owner is (which can be useful for referring back to from within the new window class as well). Setting the owner prevents the new window from going behind the parent window. The only way the new window will disappear from view is if the main window is also obscured (and the user isn't using either window).

To set the ownership of a new window you can change the property when creating the new window, as seen below.

As you can see in the image below, the main window has focus (seen by the red cross to close the program), but the pop-up window has remained visible on top of the main window.

Main window has focus but the pop-up remains on top

Main window has focus but the pop-up remains on top

Hiding the pop-up from the taskbar

When you call a new window programmatically, you also end up with another icon appearing on the taskbar alongside the main window. This is easy to prevent by setting the 'ShowInTaskbar' property to false (see below).

This will then prevent the new window from having its own icon on the taskbar which may confuse users. Alternatively, this may be a positive way of making them take notice of additional windows. The default setting for this property is true, so you only need to change this if you don't wish it to appear in the task bar.

Leave a Reply

Your email address will not be published. Required fields are marked *