Microsoft 开发人员网络 > 论坛主页 > Visual C# General > WebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and back
提出问题提出问题
 

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

  • 2006年6月26日 18:07dlyle 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

    {

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    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?

全部回复

  • 2006年6月28日 5:23Peter Huang - MSFT版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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.

    microsoft.public.inetsdk.programming.webbrowser_ctl
    microsoft.public.windows.inetexplorer.ie5.programming.components.webbrowser_ctl

    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

  • 2008年10月28日 0:13kainhart 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1b50fec6-6596-4c0a-9191-32cd059f18f7