将焦点设置为WebBroswer控件中的HTML文本框或按钮

use*_*832 6 vb.net tabs webbrowser-control setfocus

我正在WebBrowser使用VB.NET 2008 在一个控件中打开一个网站.在网站的第四页,我想通过以编程方式触发tab键来集中控件.我使用以下代码:

If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
    System.Windows.Forms.SendKeys.Send("{TAB}")
End If
Run Code Online (Sandbox Code Playgroud)

但是,我的代码无法触发Tab键.有谁知道如何使这项工作?

Jer*_*son 0

方法一

Private Sub Form_Load()
    WebBrowser1.Navigate "http://www.google.com/"
    Do
     Thread.Sleep(100)
    Loop While webBrowser1.IsBusy = True
End Sub 

Private Sub Command1_Click()
    WebBrowser1.Document.All("q").focus 'Set focus to the search text field
End Sub

Private Sub Command2_Click()
    WebBrowser1.Document.All("btnI").focus 'Set focus to the google "I Am feeling lucky button"
End Sub
Run Code Online (Sandbox Code Playgroud)

方法2

我从这个MSDN 线程将其转换为 VB.Net:Focus issues with System.Windows.Controls.WebBrowser

您需要将 ActiveElement 更改webBrowser.Document.ActiveElement.Focus()为文本框或按钮。

Public Partial Class Form1
    Inherits Form
  Public Sub New()
    InitializeComponent()
    Dim host As New WindowsFormsHost()
    im webBrowser As New WebBrowser()
    host.Child = webBrowser
    elementHost1.Child = host

    webBrowser.Navigate(New Uri("http://www.google.com"))
    Me.Activated += Function() Do
      Console.WriteLine(Me.ActiveControl)
      If webBrowser.Document <> Nothing Then
        If Me.ActiveControl = elementHost1 AndAlso webBrowser.Document.ActiveElement <> Nothing Then
          webBrowser.Document.ActiveElement.Focus()
        End If
      End If
    End Function
  End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

方法三

另一种方法可能是在 HTML 中执行此操作,例如:

OnLoad="document.myform2.mybutton.focus();"> 
Run Code Online (Sandbox Code Playgroud)