I have a form that pops up on a user's screen and has TopMost=true
, but it steals the focus. How can I get it to not steal focus when it first appears?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I tested the below code using a timer on form1 to instantiate and show form2 with form1 as owner.
In form2's Shown event I then set focus to the owner, which is the current active form.
I have a textbox on form1 and was able to continuesly write in the textbox without loosing focus during this process.
My timer code in form1:
My code in the Shown event of form2:
You can do this or simply set TopMost to false and use the override of ShowWithoutActivation as Hans Passant stated.
Edit: (Or use p/invoke as seen in Hans Passant's additional comment I missed while I wrote this)
I came across the same problem. I'm not using C# but C++. I figure this could be useful anyways:
Using windows.h:
Passing the flag SWP_NOACTIVATE to the uFlags argument worked for me.
You can set:
on Load event of that form.
It's OK with me!
Paste this code in your form:
This is what worked for me. It provides TopMost but without focus-stealing.
Remember to omit setting TopMost in Visual Studio designer, or elsewhere.
This is stolen, err, borrowed, from here (click on Workarounds):
https://connect.microsoft.com/VisualStudio/feedback/details/401311/showwithoutactivation-is-not-supported-with-topmost
You can do it like this: