Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webbrowser steals focus

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.

like image 897
Jeffrey Rasmussen Avatar asked Nov 29 '22 17:11

Jeffrey Rasmussen


2 Answers

I had the same problem:

The Webbrowser Control stole focus from the application once the URL is loaded.

This worked for me:

  1. Before Webbrowser.Navigate() method call, set the parent control of the Webbrowser to Enabled = false.
  2. At the DocumentCompleted event of the Webbrowser, reset parent control of the Webbrowser to Enabled = true.

You can't do it directly on Webbrowser because WebBrowserBase.Enabled is not supported.

Let me know if it works for you.

like image 97
Robberechts Consulting Avatar answered Dec 01 '22 08:12

Robberechts Consulting


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).

like image 23
Brian Avatar answered Dec 01 '22 07:12

Brian