C# .NET - How to force form to appear on top of all windows, even these outside application

Asked By More Than Chaos on 18-Sep-08 04:13 PM
I have few sittuations where one form calls showdialog for another form, then another form do the same for other third form. Depending on situation sometimes form can do showdialog on fourth form which displays errors. But unfortunately new forms seems to apper only in taskbar needs to be focused manually Is there any way to cape with this kind of problem?

Try this - ram kumar replied to More Than Chaos on 18-Sep-08 10:36 PM


A frequent request is how to make a window float on top of every other window. This is actually quite simple using the code below

Simply call MakeTopMost to make the window appear on top, and MakeNormal to return it to its normal behaviour

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1

Public Sub MakeNormal(hwnd As Long)
    SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
Public Sub MakeTopMost(hwnd As Long)
    SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub

Try this - ram kumar replied to More Than Chaos on 18-Sep-08 10:38 PM


Preventing a Form from Appearing in the Taskbar
Being able to display an icon for a minimized form is nice, but sometimes it's necessary to prevent a form from even appearing in the taskbar. For instance, if your application has a number of palette windows that float over a main form, it's unlikely that you'd want all but your main form to appear in the taskbar. To prevent a form from appearing in the taskbar, set the form's ShowInTaskbar property to false. If the user minimizes a form with its ShowInTaskbar property set to false, the user can still get to the application by pressing Alt+Tab, even though the program can't be accessed via the taskbar; C# won't allow the application to become completely inaccessible to the user.

Specifying the Initial Display Position of a Form
The location on the display (monitor) where a form first appears isn't random but is controlled by the form's StartPosition property. The StartPosition property can be set to one of the values in Table 5.1.

 Values for the StartPosition Property
Value  Description 
Manual  The Location property of the form determines where the form first appears. 
CenterScreen  The form appears centered in the display. 
WindowsDefaultLocation  The form appears in the Windows default location, which is toward the upper left of the display. 
WindowsDefaultBounds  The form appears in the Windows default location with its bounds (size) set to the Windows default bounds. 
CenterParent  The form is centered within the bounds of its parent form. 


Generally, it's best to set the StartPosition property of all your forms to CenterParent, unless you have a specific reason to do otherwise. For the very first form that appears in you project, you might consider using the WindowsDefaultLocation (but I generally prefer CenterScreen).

Use TopMost - Sagar P replied to More Than Chaos on 18-Sep-08 11:40 PM

Its simple use TopMost property of FORM for this.

Set this property to True, so that it will display that form on Top of all form.

This will solve your problem.

TopMost doesn't work in my case - More Than Chaos replied to Sagar P on 19-Sep-08 06:52 AM
TopMost works great but after when you call window form taskbar. You can be sure that window you called be on top, changing this property makes no difference  in my case
Question - Zimdaka Changes replied to More Than Chaos on 04-Jun-09 10:20 AM

I have C# ASP.NET webform ,then how do I set its to centerScreen at runtime?

Please Help me!!

How to force form to appear on top of all windows - Marc Fregeau replied to More Than Chaos on 28-Aug-09 02:30 PM
end of post