WebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and back<p>When I add a WebBrowser control to a Form in .NET and then set its DocumentText to include an html input element of type=&quot;text&quot; 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.</p> <p>Here's the simple code of my WebBrowser control embedded on a .NET form:</p><pre><font color="#0000ff" size=2> <p>using</font><font size=2> System;</font> </p><p><font color="#0000ff" size=2>using</font><font size=2> System.Collections.Generic;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.ComponentModel;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Data;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Drawing;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Text;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Windows.Forms;</font><font color="#0000ff" size=2> </p><p> </p><p>namespace</font><font size=2> TestWebBrowserFocusIssues </p><p>{</p> <p> [System.Runtime.InteropServices.</font><font color="#008080" size=2>ComVisibleAttribute</font><font size=2>(</font><font color="#0000ff" size=2>true</font><font size=2>)] </p><p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>partial</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>Form1</font><font size=2> : </font><font color="#008080" size=2>Form</font><font size=2> </p><p> {</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> Form1() </p><p> { </p><p> InitializeComponent(); </p><p> }</p> <p></font><font color="#0000ff" size=2> private</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Form1_Load(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e) </p><p> { </p><p> webBrowser1.ObjectForScripting = </font><font color="#0000ff" size=2>this</font><font size=2>; </p><p> webBrowser1.DocumentText = </font><font color="#800000" size=2>&quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;input name=\&quot;in1\&quot; size=\&quot;15\&quot; tabindex=\&quot;2\&quot; onblur=\&quot;window.external.BrowserCtrlLostFocus('in1')\&quot; onfocus=\&quot;window.external.BrowserCtrlGotFocus('in1')\&quot; type=\&quot;text\&quot;/&gt;&lt;input name=\&quot;result\&quot; size=\&quot;15\&quot; onblur=\&quot;window.external.BrowserCtrlLostFocus('in2');\&quot; onfocus=\&quot;window.external.BrowserCtrlGotFocus('in2')\&quot; type=\&quot;text\&quot; tabindex=\&quot;1\&quot;&gt;&lt;/body&gt;&lt;/html&gt;&quot;</font><font size=2>; </p><p> }</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> BrowserCtrlGotFocus(</font><font color="#008080" size=2>String</font><font size=2> ctrlid) </p><p> { </p><p></font><font color="#008080" size=2> Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;Ctrl&quot;</font><font size=2>+ctrlid+</font><font color="#800000" size=2>&quot;Got Focus&quot;</font><font size=2>); </p><p> }</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> BrowserCtrlLostFocus(</font><font color="#008080" size=2>String</font><font size=2> ctrlid) </p><p> { </p><p></font><font color="#008080" size=2> Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;Ctrl&quot;</font><font size=2>+ctrlid+</font><font color="#800000" size=2>&quot;Lost Focus&quot;</font><font size=2>); </p><p> } </p> <p> }//end of class Form1 </p><p>}//end of namespace</p><p> </p><p>What I can't figure out is why the WebBrowser control appears to handle this different from Internet Explorer itself or </p><p>other standard browsers that return the focus to the input text field that had it when you do an application </p><p>switch via ALT+TAB?</p><p> </p><p>Any ideas or suggestions?</p></font></pre>© 2009 Microsoft Corporation. All rights reserved.Tue, 28 Oct 2008 00:13:35 Zc6f83ce5-1525-4d31-8c14-05e8a301d92ahttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#c6f83ce5-1525-4d31-8c14-05e8a301d92ahttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#c6f83ce5-1525-4d31-8c14-05e8a301d92adlylehttp://social.msdn.microsoft.com/Profile/en-US/?user=dlyleWebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and back<p>When I add a WebBrowser control to a Form in .NET and then set its DocumentText to include an html input element of type=&quot;text&quot; 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.</p> <p>Here's the simple code of my WebBrowser control embedded on a .NET form:</p><pre><font color="#0000ff" size=2> <p>using</font><font size=2> System;</font> </p><p><font color="#0000ff" size=2>using</font><font size=2> System.Collections.Generic;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.ComponentModel;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Data;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Drawing;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Text;</font><font color="#0000ff" size=2> </p><p>using</font><font size=2> System.Windows.Forms;</font><font color="#0000ff" size=2> </p><p> </p><p>namespace</font><font size=2> TestWebBrowserFocusIssues </p><p>{</p> <p> [System.Runtime.InteropServices.</font><font color="#008080" size=2>ComVisibleAttribute</font><font size=2>(</font><font color="#0000ff" size=2>true</font><font size=2>)] </p><p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>partial</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>Form1</font><font size=2> : </font><font color="#008080" size=2>Form</font><font size=2> </p><p> {</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> Form1() </p><p> { </p><p> InitializeComponent(); </p><p> }</p> <p></font><font color="#0000ff" size=2> private</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Form1_Load(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e) </p><p> { </p><p> webBrowser1.ObjectForScripting = </font><font color="#0000ff" size=2>this</font><font size=2>; </p><p> webBrowser1.DocumentText = </font><font color="#800000" size=2>&quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;input name=\&quot;in1\&quot; size=\&quot;15\&quot; tabindex=\&quot;2\&quot; onblur=\&quot;window.external.BrowserCtrlLostFocus('in1')\&quot; onfocus=\&quot;window.external.BrowserCtrlGotFocus('in1')\&quot; type=\&quot;text\&quot;/&gt;&lt;input name=\&quot;result\&quot; size=\&quot;15\&quot; onblur=\&quot;window.external.BrowserCtrlLostFocus('in2');\&quot; onfocus=\&quot;window.external.BrowserCtrlGotFocus('in2')\&quot; type=\&quot;text\&quot; tabindex=\&quot;1\&quot;&gt;&lt;/body&gt;&lt;/html&gt;&quot;</font><font size=2>; </p><p> }</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> BrowserCtrlGotFocus(</font><font color="#008080" size=2>String</font><font size=2> ctrlid) </p><p> { </p><p></font><font color="#008080" size=2> Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;Ctrl&quot;</font><font size=2>+ctrlid+</font><font color="#800000" size=2>&quot;Got Focus&quot;</font><font size=2>); </p><p> }</p> <p></font><font color="#0000ff" size=2> public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> BrowserCtrlLostFocus(</font><font color="#008080" size=2>String</font><font size=2> ctrlid) </p><p> { </p><p></font><font color="#008080" size=2> Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;Ctrl&quot;</font><font size=2>+ctrlid+</font><font color="#800000" size=2>&quot;Lost Focus&quot;</font><font size=2>); </p><p> } </p> <p> }//end of class Form1 </p><p>}//end of namespace</p><p> </p><p>What I can't figure out is why the WebBrowser control appears to handle this different from Internet Explorer itself or </p><p>other standard browsers that return the focus to the input text field that had it when you do an application </p><p>switch via ALT+TAB?</p><p> </p><p>Any ideas or suggestions?</p></font></pre>Mon, 26 Jun 2006 18:07:25 Z2006-06-26T18:07:25Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#6c4d9260-ff25-4f4a-bae3-5f7407717628http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#6c4d9260-ff25-4f4a-bae3-5f7407717628Peter Huang - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20Huang%20-%20MSFTWebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and back<p>Hi ,</p> <p>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.</p> <p>microsoft.public.inetsdk.programming.webbrowser_ctl <br>microsoft.public.windows.inetexplorer.ie5.programming.components.webbrowser_ctl</p> <p>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.</p> <p>Thanks for your understanding! </p> <p>If you still have any concern, please feel free to post here.</p> <p>Best regards,<br>Peter Huang</p>Wed, 28 Jun 2006 05:23:22 Z2006-06-28T05:23:22Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#db93928d-a73b-407c-a1b2-24da769b701fhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c6f83ce5-1525-4d31-8c14-05e8a301d92a#db93928d-a73b-407c-a1b2-24da769b701fkainharthttp://social.msdn.microsoft.com/Profile/en-US/?user=kainhartWebBrowser Control active element doesn't regain focus after ALT+TABbing to another app and backLooks 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 &quot;wake it up&quot; but this unfortunately has also caused other side effects. For more information please see my post below.<br><br><br><a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1b50fec6-6596-4c0a-9191-32cd059f18f7" title="Focus issues with System.Windows.Controls.WebBrowser">Focus issues with System.Windows.Controls.WebBrowser</a><br><br>http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1b50fec6-6596-4c0a-9191-32cd059f18f7<br> Tue, 28 Oct 2008 00:13:35 Z2008-10-28T00:13:35Z