Bug with WebBrowser control and focus. Any workarounds?

No matter how hard I try, I can't get the WebBrowser control to truely gain focus. Even after using WebBrowser1.Focus () in my program, when I run my program the WebBrowser control doesn't really get focus until I click on it with the mouse. How do I know this Well, the arrow keys don't work to scroll the web page, nor do PageUp or PageDown, until I click on the WebBrowser control with the mouse. Same goes for moving through the links with the TAB key.

Since SP1 for Visual Basic 2005 didn't change things, is there a way I can work around this problem, short of clicking on the control with the mouse



Answer this question

Bug with WebBrowser control and focus. Any workarounds?

  • shinoy

    Thanks nobugz. I tried your code, but unless I'm pasting it in the wrong place (and I've tried a few places), it does nothing. I still need to click on the WebBrowser area before the keystrokes will work. :(
  • Duane Haas

    I have the same issue as mentioned above. Offered code doesn't help... So the conclusion: this IS the bug. Not feature. Personally I'm interested in gaining focus by WebBrowser control automatically on FormLoad. Tried so many ways, but nothing help. Still need to click inside WB control.
  • shine46055

    Is it posible you and nobugs are looking at different issues Have you tried WebBrowser.focus and webbrowser.select They work for me. Also have you tried looking at you tab indexes Set the webbrowser to zero.



  • Ross Watson

    It's not a bug, its a feature. Add this code to your form to fix it:

    Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    '--- Make form navigation keys act like input keys when the focus is on the web browser
    If ReferenceEquals(Me.ActiveControl, Me.WebBrowser1) Then
    If keyData = Keys.Up Or keyData = Keys.Down Or keyData = Keys.Left Or keyData = Keys.Right Then Return True
    If keyData = Keys.PageDown Or keyData = Keys.PageUp Then Return True
    If keyData = Keys.Tab Then Return True
    End If
    Return MyBase.IsInputKey(keyData)
    End Function



  • rsacristanp

     

    I have the same problem. I have a large sophisticated project. This is the last known glitch in the entire project. Also, it's quite possible this has been introduced by SP1. At any rate.....

    I have a webbrowser control used as an editor. There is a text box control below the webbrowser control for document titles which has a press event. the last thing I do in the keypress event is webrowser.Focus

    This does work. I've tried timer events 50 ms after the keypress to set the focus. That doesn't work.

    I've set focus to the HTMLDocument contained by the webbrowser.

    This is the last bug/annoyance in a really large and complex problem and it's driving me crazy.

     



  • Ekta

    Odd, it worked fine when I tested it. Location isn't important, anywhere inside the form's code.


  • &#169&#59; Ţĩмό Şąļσмāĸ

    Like I said I've tried many ways including yours. Same result. But thanks anyway.

    Some details: My WB loads web-page on form load so I try to gain focus on WebBrowser1_DocumentCompleted event. WB TabIndex is 0 and .focus () or/and .select() have no effect.

    P.S> Of course maybe some other elements (TextBoxes, Buttons etc.) causing that strange behaviour, but we're still there, where we were...


  • robinjam

    BUMP. Any info on this, please
  • Bug with WebBrowser control and focus. Any workarounds?