2
votes

I'm making a Windows Forms application with VB.Net, Visual Studio 2015.
The Form has a WebBrowser control and other controls.

Whenever pressing the TAB key on keyboard, it always focuses on a html element loaded in the WebBrowser control first. Then pressing the TAB key again, the focus is switched between the HTML elements in the WebBrowser control.

Till ending up switching in all HTML elements, the focus doesn't switch to other controls in the Form.
Though I set .TabIndex = 1000 and .TabStop = false in the WebBrowser control, it always focuses on a html element loaded in the WebBrowser control first, always first.

So, I want to disable focusing on the WebBrowser control by pressing the TAB key or to disable the TAB key function in the Form entirely.

1
exactly, It always focus on a html elements in web browser control first. when pressing tap key.bbond
It is not very clear what you are asking. If you do not want the browser control to be a tabstop then set its TabStop property to false.TnTinMn
I tried to make sense of your question and improve its formatting. If any of you thinks that I made a mistake, please feel free to correct my edit.zx485
though I already set 'tapstop = false' in web browser control, It always focuses on a html element loaded in web browser control first.bbond

1 Answers

0
votes

I have to get the answer done in VB.NET soon, but for now here's the C# version of it:

First, an extended web browser control, that you'll have to use on the form, with a custom event when the tab key is pressed.

Here we call the TabStop = false to ensure this key gets processed. Similar reasoning on WebBrowserShortcutsEnabled.

Then, we capture on the HTML Body, the key press event.

If the key code is a 9 (tab), we fire our event.

public class WebBrowserExtended : System.Windows.Forms.WebBrowser
{

    protected virtual void OnTabKeyEvent(EventArgs e)
    {
        EventHandler handler = TabKeyEvent;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public event EventHandler TabKeyEvent;

    public WebBrowserExtended() : base()
    {
        this.TabStop = false;
        this.WebBrowserShortcutsEnabled = false;
    }

    protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
    {
        base.OnDocumentCompleted(e);
        if (this.Document.Body != null)
            this.Document.Body.KeyDown += new HtmlElementEventHandler(Body_KeyDown);
    }

    private void Body_KeyDown(Object sender, HtmlElementEventArgs e)
    {
        if (e.KeyPressedCode == 9 && !e.CtrlKeyPressed)
        {
            this.OnTabKeyEvent(e);
            e.BubbleEvent = false;
        }
    }
}

And here's is your event handler:

  private void webBrowser1_TabKeyEvent(object sender, EventArgs e)
    {
        var controls = new List<Control>(this.Controls.Cast<Control>());
        var nextControl = controls.Where(c => c.TabIndex > webBrowser1.TabIndex).OrderBy(c => c.TabIndex).FirstOrDefault();
        if (nextControl != null)
            nextControl.Focus();
        else
            controls.OrderBy(c => c.TabIndex).FirstOrDefault().Focus();
    }

And here's the VB version of the control:

Public Class WebBrowserExtended
    Inherits System.Windows.Forms.WebBrowser

    Protected Overridable Sub OnTabKeyEvent(ByVal e As EventArgs)
        RaiseEvent TabKeyEvent(Me, e)
    End Sub

    Public Event TabKeyEvent As EventHandler

    Public Sub New()
        MyBase.New()
        Me.TabStop = False
        Me.WebBrowserShortcutsEnabled = False
    End Sub

    Protected Overrides Sub OnDocumentCompleted(ByVal e As WebBrowserDocumentCompletedEventArgs)
        MyBase.OnDocumentCompleted(e)
        If Me.Document.Body IsNot Nothing Then
            AddHandler Me.Document.Body.KeyDown, AddressOf Body_KeyDown
        End If
    End Sub

    Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        If e.KeyPressedCode = 9 AndAlso Not e.CtrlKeyPressed Then
            Me.OnTabKeyEvent(e)
            e.BubbleEvent = False
        End If
    End Sub
End Class

And the VB event handler:

Private Sub WebBrowser1_TabKeyEvent(sender As Object, e As EventArgs) Handles WebBrowser1.TabKeyEvent

        Dim controls = New List(Of Control)(Me.Controls.Cast(Of Control))
        Dim nextControl = controls.Where(Function(c)
                                             Return c.TabIndex > WebBrowser1.TabIndex
                                         End Function).OrderBy(Function(c)
                                                                   Return c.TabIndex
                                                               End Function).FirstOrDefault()
        If Not controls Is Nothing Then
            nextControl.Focus()
        Else
            controls.OrderBy(Function(c)
                                 Return c.TabIndex
                             End Function).FirstOrDefault().Focus()
        End If


    End Sub