WebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and back RRS feed

  • Question

  • When I add a WebBrowser control to a Form in .NET and then set its DocumentText to include an html input element of type="text" there appears to be a slight bug in how this text field retains focus when switching between apps using ALT+TAB.  If you set focus to the text field and then hit ALT+TAB to go to another application and then hit ALT+TAB and select your original application again, the embedded WebBrowser control doesn't set focus back to the text field that you just left. However, strangely enough, if you do this ritual twice in a row, the text field mysteriously regains focus as you would have expected on the first run through of switching back and forth between the applications.

    Here's the simple code of my WebBrowser control embedded on a .NET form:


    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;


    namespace TestWebBrowserFocusIssues



    public partial class Form1 : Form


    public Form1()




    private void Form1_Load(object sender, EventArgs e)


    webBrowser1.ObjectForScripting = this;

    webBrowser1.DocumentText = "<html><head></head><body><input name=\"in1\" size=\"15\" tabindex=\"2\" onblur=\"window.external.BrowserCtrlLostFocus('in1')\" onfocus=\"window.external.BrowserCtrlGotFocus('in1')\" type=\"text\"/><input name=\"result\" size=\"15\" onblur=\"window.external.BrowserCtrlLostFocus('in2');\" onfocus=\"window.external.BrowserCtrlGotFocus('in2')\" type=\"text\" tabindex=\"1\"></body></html>";


    public void BrowserCtrlGotFocus(String ctrlid)


    Console.WriteLine("Ctrl"+ctrlid+"Got Focus");


    public void BrowserCtrlLostFocus(String ctrlid)


    Console.WriteLine("Ctrl"+ctrlid+"Lost Focus");


    }//end of class Form1

    }//end of namespace


    What I can't figure out is why the WebBrowser control appears to handle this different from Internet Explorer itself or

    other standard browsers that return the focus to the input text field that had it when you do an application

    switch via ALT+TAB?


    Any ideas or suggestions?

    Monday, June 26, 2006 6:07 PM

All replies

  • Hi ,

    For WebBrowser control related issue, please post this question in the newsgroup below. The newsgroups is primarily for issues involving IE and WebBrowser control. We recommend posting appropriately so you will get the most qualified pool of respondents, and so other partners who regularly read the newsgroups can either share their knowledge or learn from your interaction with us.


    If the issue is urgent, you may want to work with Microsoft Customer Service and Support (CSS) for a faster resolution. Once you open a Support incident with Microsoft CSS, a dedicated Support Professional can work with you in a more efficient manner.

    Thanks for your understanding!

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang

    Wednesday, June 28, 2006 5:23 AM
  • Looks like I'm running into the same issue. Have you had any luck on a solution so far? We have experimented using the SendKeys method to send {TAB} key presses to the WebBrowser control to in a sense "wake it up" but this unfortunately has also caused other side effects. For more information please see my post below.

    Focus issues with System.Windows.Controls.WebBrowser
    Tuesday, October 28, 2008 12:13 AM