我正在使用 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/