vb.net 将焦点设置为 WebBroswer 控件中的 HTML 文本框或按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11174903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set the focus to a HTML Textbox or Button in a WebBroswer control
提问by user1473832
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. I am using the following code:
我正在WebBrowser
使用 VB.NET 2008 在控件中打开一个网站。在网站的第四页上,我想通过以编程方式触发 Tab 键来聚焦控件。我正在使用以下代码:
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. Does anyone know how to make this work?
但是,我的代码无法触发 tab 键。有谁知道如何使这项工作?
回答by Jeremy Thompson
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.
您需要将 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
Method 3
方法三
Another way might be to do it in the HTML, eg:
另一种方法可能是在 HTML 中进行,例如:
OnLoad="document.myform2.mybutton.focus();">
回答by RedDm
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
回答by RedDm
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()
回答by coolbotic
To focus the select element by using the focus function in vb.net. For exsample,
使用 vb.net 中的 focus 函数聚焦 select 元素。例如,
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
回答by Anony DealSmoke
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
制作一个文本框,然后如果您想发送垃圾邮件,它很容易检测您每次编写和发送的打字机