Enable Tab Key in my Web Browser Control

2019-09-04 01:27发布

问题:

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.

Cheers.

回答1:

Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.

For that change the WebBrowser contructor from

new System.Windows.Forms.WebBrowser();

to

new Microsoft.Office.Tools.Excel.Controls.WebBrowser();  

You would need to add references to your project: Project/Add Referernce/Extensions select Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities

Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx



回答2:

You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.



回答3:

It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.

Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
        TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)

        Me.Focus()

        TestWebBrowser.Navigate(url)

        WaitForPageLoad()

        TestWebBrowser.TabIndex = 0
        TestWebBrowser.TabStop = True
    End Sub

Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted

        ' Now set the focus on the Web Browser Control
        TestWebBrowser.Focus()
End Sub