我是Visual Basic的新手,我使用WebBrowser
组件在visual basic中有一个简单的应用程序,加载的网页有一个按钮(html),我想启用一个按钮按下那个html按钮的视觉基础,我不知道怎么做到这一点,我读到我可以做到这一点而不是如何,我在SO和youtube搜索过这里,但我没有找到我所做的事情。在寻找,能帮助我实现这个目标吗?谢谢。
<button type="button" id="login">Login</button>
答案 0 :(得分:1)
我可以为你简化:),非常简单。
我使用的是以下内容: 1- HTML页面“Index.html”,带有一个名为“CheckStat”的简单javascript函数来更改按钮值。 2-表格,带1个按钮,1个WebBrowser和1个计时器。
“的index.html”
<!DOCTYPE html>
<html>
<head>
<title>HTML - VB.NET</title>
<script>
function CheckStat(){
document.getElementById("login").innerHTML = "clicked";
}
</script>
</head>
<body>
<button type="button" id="login" onclick="CheckStat();">Login</button>
</body>
</html>
这是我在Visual Studio中所做的,首先这是我的代码: “Form1.vb的”
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'I am using localhost, it's not required you can simply put your HTML file on your Desktop or you can get it from any website.
WebBrowser1.Url = New Uri("http://localhost/stackoverflow.com/AN03/index.html")
'Enalbe Timer
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
'Get that our HTML button, as you see i am using GetElementById("HTML BUTTON ID HERE")....
Dim loginbtn = WebBrowser1.Document.GetElementById("login")
If loginbtn.InnerHtml = "clicked" Then
'Disable what button you want to disable...
Button1.Enabled = False
'Change HTML text to something else because every 100 interval it checks again and again...
loginbtn.InnerHtml = "click me"
'This message box just shows you were clicked or not. you can simply remove it
MessageBox.Show("Clicked me is enabled")
End If
Catch ex As Exception
'for any problem, it shows this message box to the user and tells him/her what was wrong....
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
因为你说我是VB.net的新手,让我告诉你发生了什么。当您单击Button1时,WebBrowser将为您加载该文件,并启用Timer1。
现在浏览器加载了页面,timer1每隔100个间隔运行该代码1秒= 1000个间隔,首先它从HTML页面中搜索其ID等于“login”的所有元素,然后返回名为“loginbtn”的变量。
然后检查其值(HTML按钮上显示的文本或标题)如果等于“点击”则表示用户单击HTML按钮。
然后它禁用Button(VB的Button),将HTML'S Button值更改为“再次点击我”并显示消息框...
对于任何问题,它会向用户显示此消息框并告诉他/她出了什么问题....
是的,你可以做到,但我使用计时器,因为它每隔1分钟或1小时或任何时候检查它...我更新了你,我创建了这个功能请告诉我,如果你不了解它或其他任何东西.... 你可以禁用或删除你的计时器,添加一个按钮,并将这个简单的代码添加到它,我把它放在Button2 ... 还有一件事,如果你看到我将我的功能创建为私有,那么你就不能从任何其他形式或类等调用。 您可以将其更改为公共,以便您可以在应用中的任何位置使用它。 我怎么称它呢? 。 像这样:Form1.CheckStatByid(“login”)。更新的代码是:
Dim _Stat As Boolean = False
Private Function CheckStatByid(ByVal htmlid)
' Check sended paramiter
' IF it has no value then we need to end our function,
' Else we can check its value...
If htmlid <> Nothing Then
Try
'Get that our HTML button, as you see i am using GetElementById("HTML BUTTON ID HERE")....
Dim loginbtn = WebBrowser1.Document.GetElementById(htmlid)
'Check loginbtn if it has a value or NOT
If loginbtn <> Nothing Then
If loginbtn.InnerHtml = "clicked" Then
'Change HTML text to 'click me again', but you can change it to anything else...
loginbtn.InnerHtml = "click me again"
_Stat = True
Else
'If we have some value (ID) returns from our HTML, but it doesn't match our ID which is "login"
MessageBox.Show("loginbtn variable has deffrent id OR button is not clicked yet. and we end the try...catch")
Exit Try
End If
Else
'We don't find any elements from our HTML page...
MessageBox.Show("loginbtn variable has no value. and we end the try...catch")
Exit Try
End If
Catch ex As Exception
'for any proble it shows this message box to user and tell him/her what was wrong....
MessageBox.Show(ex.Message)
End Try
Else
' We don't have any ID to check...
MessageBox.Show("Please recall the function with an ID...")
End If
' Retuen _Stat : if the IF statment was true then it returns TRUE, else it returns FASLE
Return _Stat
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' How to use our function? its easy
' check it with some value.
If CheckStatByid("login") = True Then
' User Clicked the HTML button, we can now disable our button or do any thing else...
Button1.Enabled = False
Else
' User doesn't clicked the button or any other stat has been returned from our function.
' Message box will show for any stat...
' if you like, you can simply remove those message box from function and put them here...
MessageBox.Show("HTML page is not loaded or user doesn't clicked...")
End If
End Sub
希望你能理解。我很抱歉任何语法或拼写错误。 ...