vb.net - 将焦点设置到 Web 浏览器控件中的 HTML 文本框和按钮

标签 vb.net tabs webbrowser-control setfocus

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

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

但是,我的代码无法触发 Tab 键。有谁知道如何做到这一点?

最佳答案

方法一

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

方法2

我将它从 MSDN thread: Focus issues with System.Windows.Controls.WebBrowser 转换为 VB.Net

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

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

方法3

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

OnLoad="document.myform2.mybutton.focus();"> 

关于vb.net - 将焦点设置到 Web 浏览器控件中的 HTML 文本框和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174903/

相关文章:

google-chrome - pageAction 的当前 TabID

Angular 7 检测浏览器选项卡已激活

wpf - WebBrowser 无法正确显示内容

Vb.net Property 获取和设置没有私有(private)变量的值

database - 具有未知外键的 SQLite 关系 INSERT

.net - 取消按钮需要第二次按下

c# - 无法在新的 Chrome 浏览器标签页中打开 pdf

c# - WPF - 无法在 WebBrowser 控件中从本地计算机打开文件

javascript - 无法在 Webbrowser #Vb.net 上通过其已知的元素 ID 单击按钮

css - 制作 pdf 时无法让 CSS 在 iTextSharp (5.4.3) 中工作