-3

In vb.net or c# WinForms, how would you make a form topmost over the other forms in the project, but not over the windows of other applications?

Using form.topmost = True puts the form above other applications.

EDIT

I am NOT looking for a splash screen.

Below is an example of the intended behavior of this form. It remains on top of everything else in the application, and you can interact with it and the form behind it.

enter image description here

Bigbob556677
  • 1,805
  • 1
  • 13
  • 38
  • Like making it modal? Or do you want to access forms behind it while it is up? – Broots Waymb Mar 16 '18 at 14:34
  • Like a splash screen that loads in front of your other forms but not over another app like Chrome or Visual Studio. @BrootsWaymb – Bigbob556677 Mar 16 '18 at 14:35
  • @BrootsWaymb No. A splash screen was just an arbitrary example. – Bigbob556677 Mar 16 '18 at 14:37
  • [How do I make a form modal](https://stackoverflow.com/questions/2503079/how-do-i-make-a-form-modal-in-windows-forms) – Jacob H Mar 16 '18 at 14:37
  • I'm assuming you can still get most of your answers from that linked duplicate. Unless you're looking for a modal form, that a little easier. If it's something else you'll need to be a little more clear. – Broots Waymb Mar 16 '18 at 14:40
  • @BrootsWaymb Thanks but that link is not actually what I need. it's not a modal form. as the code executed in the first form must continue to execute. just simply a form that's called using "form.show()" and will remain topmost over the other forms but not topmost over the whole desktop. – Bigbob556677 Mar 16 '18 at 14:49
  • That doesn't mean it shouldn't be a modal form. If you wan't forms to be executing code simultaneously, you'll need some sort of `BackgroundWorker`. – Broots Waymb Mar 16 '18 at 15:02
  • Take a look at the documentation for [Form.Owner-Remarks Section](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner(v=vs.110).aspx) and [Form.Show Method (IWin32Window)](https://msdn.microsoft.com/en-us/library/szcefbbd(v=vs.110).aspx) to see if that is behavior you are seeking. – TnTinMn Mar 16 '18 at 16:35
  • @BrootsWaymb If you could note the latest edit. In a popular CAD software, this properties form is above the drawing form and you can make changes to properties and also use the form behind it BUT the properties form still stays on top. This is the exact desired behavior i want. Thanks – Bigbob556677 Mar 20 '18 at 17:56

3 Answers3

1

To bring a form on top of other forms withon an application, you can use the BringToFront method.

Application.OpenForms["MyForm"].BringToFront();

The other forms will be accessible to the user.

Reuben Mallaby
  • 5,740
  • 4
  • 47
  • 45
1

The topmost=true should work fine for your application. There must be a user error occurring.

dsealy1986
  • 26
  • 2
0

You can use the SetWindowPos method to bring a window to the front without activating it. You could call this in a timer to keep it on top (but that will probably put it in front of other apps, so you would only want to do that if you were the activate application) or you would have to detect when other forms fire the Activated event and then call this.

internal const int SWP_NOMOVE = 0x0002;
internal const int SWP_NOSIZE = 0x0001;
internal const int SWP_SHOWWINDOW = 0x0040;
internal const int SWP_NOACTIVATE = 0x0010;
internal const int SWP_NOOWNERZORDER = 0x0200;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

Call it with: SetWindowPos(form.Handle,0,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW|SWP_NOACTIVATE);