How do you keep a .NET form always above another application?
.net, c#, layout, window, winforms
Solution
Have you tried making the C++ window the owner of the .NET form, before showing it? Window handles are session-global, so you can communicate them via IPC. And the Show method accepts any `IWin32Window` as owner. From the MSDN page:
When a form is owned by another form, it is closed or hidden with the owner form. For example, consider a form named Form2 that is owned by a form named Form1. If Form1 is closed or minimized, Form2 is also closed or hidden. Owned forms are also never displayed behind their owner form.
Response to comment:
Maybe this wasn't clear enough: The `Owner` property is of type `Form`, so you can't assign it any `IWin32Window` object once the form is visible. But `Form.Show` does accept an `IWin32Window` owner parameter - so you can set the owner to any window when you first show your form. I've looked at `Form.Show` in Reflector, as far as I can see, it should handle any `IWin32Window` parameter, even if it's not a `Form`.
If you need to set the window owner later than the first call to `Form.Show`, then you'll probably have to PInvoke `SetWindowLong` (as described in this answer). That's what `Form.Show` does, too.
Problem
I have a C++ app that is running as the main application. A .NET app is also running side by side and I have interprocess communication via sockets. How do I maintain the .NET main form above the C++ app? Methods: Currently I've set `TopMost = true` but this causes problems when you open another application : the .NET app appears above all other apps. Another way would be to check when you `LostFocus`, if the user has clicked on the C++ app then you bring the .NET app to the foreground, and if the user switched to another app then do nothing. Any ideas? Has this ever been done before? I've seen a similar effect in Paint.NET, where the tool palettes remain on top of the other window.