[英]Set the focus to a HTML Textbox or Button in a WebBroswer control
I am opening a website in a WebBrowser
control using VB.NET 2008. On the fourth page of the website, I want to focus the control by triggering the tab key programmatically.我正在使用 VB.NET 2008 在
WebBrowser
控件中打开一个网站。在网站的第四页,我想通过以编程方式触发 Tab 键来聚焦控件。 I am using the following code:我正在使用以下代码:
If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
However, my code is unable to trigger the tab key.但是,我的代码无法触发 tab 键。 Does anyone know how to make this work?
有谁知道如何使这项工作?
Method 1方法一
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
Method 2方法二
I converted it to VB.Net from this MSDN thread: Focus issues with System.Windows.Controls.WebBrowser我从这个MSDN 线程将它转换为 VB.Net:Focus issues with System.Windows.Controls.WebBrowser
You will need to change the ActiveElement in webBrowser.Document.ActiveElement.Focus()
to the textbox or button.您需要将
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
Method 3方法三
Another way might be to do it in the HTML, eg:另一种方法可能是在 HTML 中进行,例如:
OnLoad="document.myform2.mybutton.focus();">
lets say that the html of youre page is:假设您页面的 html 是:
<button id="btn">Ok</button><input id="txt">
you can set focus in this way:您可以通过这种方式设置焦点:
If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
webbrowser1.document.getelementbyid("btn").focus()
webbrowser1.document.getelementbyid("txt").focus()
End If
Another way:其它的办法:
use the GetElementsByTagName(TagName)
使用
GetElementsByTagName(TagName)
lets say that your html is:假设您的 html 是:
<button>no</button>
<button>no</button>
<button onclick='alert(1);'>--focus me!--</button>
<button>no</button>
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In Elems
If elem.InnerHtml = "--focus me!--" Then
elem.Focus()
elem.InvokeMember("click")
End If
Next
another one:另一个:
Dim num As Integer = 1
Dim elms As HtmlElementCollection
Dim wb As WebBrowser = WebBrowser1
elms = wb.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In elms
If elem.Id = "" Then
elem.Id = "button" & num.ToString
num = num + 1
End If
Next
WebBrowser1.Document.GetElementById("button3").Focus()
To focus the select element by using the focus function in vb.net.使用 vb.net 中的 focus 函数来聚焦 select 元素。 For exsample,
例如,
Me.WebBrowser1.Document.All.Item("password").Focus()
This will put the focus on the element called password!这会将焦点放在名为 password 的元素上!
Use Me.WebBrowser1.Document.All.Item("YOURelement")
to find the correct element and then add .Focus()
to focus on the one you want: :D使用
Me.WebBrowser1.Document.All.Item("YOURelement")
找到正确的元素,然后添加.Focus()
以专注于您想要的元素::D
Do this Me.WebBrowser1.Document.All.Item(textbox1.text).Focus()这样做 Me.WebBrowser1.Document.All.Item(textbox1.text).Focus()
make a Textbox and then if you want to Spambot it easy Detects everytime your Typ you Write and Send制作一个文本框,然后如果你想发送垃圾邮件,它很容易在你每次输入和发送时检测到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.