Webbrowser steals focus
Solution 1
I had the same problem:
The Webbrowser Control stole focus from the application once the URL is loaded.
This worked for me:
- Before
Webbrowser.Navigate()
method call, set the parent control of the Webbrowser toEnabled = false
. - At the
DocumentCompleted
event of the Webbrowser, reset parent control of the Webbrowser toEnabled = true
.
You can't do it directly on Webbrowser because WebBrowserBase.Enabled
is not supported.
Let me know if it works for you.
Solution 2
You could try disabling it globally via the SystemParametersInfo api. Use SPI_SETFOREGROUNDLOCKTIMEOUT
. Setting foreground lockout is a global settings, so you will want to clear this setting when you're done. A more permanent solution is to change HKCU\Control Panel\Desktop\ForegroundLockTimeout registry key. See also this discussion on social.msdn (specifically, billb08's answer).
Solution 3
I guess WebBrowser
acquires the focus after a page is loaded by calling Navigate
(or the Click
method of an HtmlElement
, which causes navigation). The focus could be given back to the control on the window (the TextBox
) in the DocumentComplete
event handler of the WebBrowser
, but this is very difficult:
When would you determine which control owned the focus originally? Before calling
Navigate
? This is not enough, because the user can move to another control after callingNavigate
, but before handlingDocumentComplete
.AFAIK setting the focus to a
TextBox
will select its whole content, so you will have to put the cursor back to its original position. But when would you store the original position? Same problem.There can be more than one DocumentComplete event after a single
Navigate
(orClick
).
A possible solution would be to create a separate application for your hidden WebBrowser
. This second application would be invisible, and could communicate with the original GUI application using some InterProcess Communication (IPC) technique. Because the WebBrowser
in this case would run in a different process, you would have a better chance not to lose lose the focus and bother the user.
Solution 4
it's a very complex problem to fix, and should be revised by microsoft, an app just stealing the focus is not logical, it does depend on what the website is doing though. I had to resort to a CBT filter, see http://msdn.microsoft.com/en-us/magazine/cc188966.aspx, and filter out unwanted HCBT_ACTIVATE and HCBT_SETFOCUS (return 1;). You can use GetWindowClass(wParam) to see what's going on.
Even above didn't entirely work, the app window would still pop to the front temporarily so worked around that using SetWindowPos HWND_TOPMOST and HWND_NOTOPMOST on the window currently in foreground. The HCBT_SETFOCUS gets hit 2 or 3 times so on 1st set HWND_TOPMOST and last set HWND_NOTOPMOST. Count how many classname == "Internet Explorer_Server" which should be 2 (or possibly depends on website?), the other is "Shell Embedding" but doesn't always occur. Hope it helps.
Solution 5
I was looking at all the other answers to this question and they weren't working for me, but i saw the one about settings Browser.Parent.Enabled = false; i tried so and got an error, so i tried this instead it just came to mind.
Browser.Parent = new Control();
Browser.Parent.Enabled = false;
And now the problem is completely gone, it does not take away focus anymore. I am using the web browser class as a variable, it is not on my form. well this worked for me try it, this seemed to be a 100% solution.
Jeffrey Rasmussen
Updated on June 13, 2022Comments
-
Jeffrey Rasmussen almost 2 years
I'm using webbrowser control in my winforms app (c#). And when it is doing automation things, I'm losing focus control from the window I was working with. Webbrowsers' form doesn't show up also, I just lose focus from the contol. I now writing this message I have to click into textbox again and again...
How to disable such behaviour in webbrowser?
I create invisible webbrowser like that:
var br = new WebBrowser(); br.Visible = false; br.ScriptErrorsSuppressed = true;
Please advise.
-
Ravi M Patel over 7 yearsOne could just use Form.Activate() and then Control.Focus() methods to get focus back.
-
Tom about 6 yearsUnfortunately - the webcontrol still steals focus even if running in a another process
-
Nick Allan over 5 yearsRavi's solution is more reliable.