Jeffrey Rasmussen Jeffrey Rasmussen - 2 months ago 48
C# Question

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.

Answer

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.