我是新的Visual Basic,我必須使用Visual Basic中的WebBrowser
組件,加載的網頁上有一個按鈕(HTML)的簡單應用程序,我想啓用一個可視化基本按鈕時,按下該HTML按鈕,我不知道如何實現這一點,我讀我可以做到這一點,但不是如何,我在SO和YouTube搜索這裏,但我沒有找到我在找什麼因爲,你能幫我實現這個嗎?,謝謝。點擊HTML按鈕,啓動按鈕在Visual Basic
<button type="button" id="login">Login</button>
我是新的Visual Basic,我必須使用Visual Basic中的WebBrowser
組件,加載的網頁上有一個按鈕(HTML)的簡單應用程序,我想啓用一個可視化基本按鈕時,按下該HTML按鈕,我不知道如何實現這一點,我讀我可以做到這一點,但不是如何,我在SO和YouTube搜索這裏,但我沒有找到我在找什麼因爲,你能幫我實現這個嗎?,謝謝。點擊HTML按鈕,啓動按鈕在Visual Basic
<button type="button" id="login">Login</button>
我可以簡化爲你:),它很容易。
我使用的是以下內容: 1- HTML頁面「Index.html」帶有一個名爲「CheckStat」的簡單javascript函數來更改您的按鈕值。帶有1個按鈕,1個WebBrowser和1個定時器的2-形式。
「的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間隔,首先搜索其ID等於「login」的HTML頁面中的所有元素,並返回到名爲「loginbtn」的變量。
然後它會檢查它的值(即在HTML按鈕上顯示的文本或標題),如果它等於「點擊」,則意味着用戶點擊HTML按鈕。
然後禁用按鈕(VB的按鈕),改變HTML上的按鈕值「再次點擊我」,並顯示消息框...
對於任何問題,它顯示此消息框給用戶,並告訴他/她出什麼事....
是的,你可以做到這一點,但我用定時器,因爲它檢查它的每一個,即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
希望你能理解它。我很抱歉有任何語法或拼寫錯誤。 ...
這不是一個Visual Studio的問題。 – EJoshuaS
對不起我沒有時間將其轉換爲VB,但這裏是一個C#解決這個問題:http://ryanfarley.com/blog/archive/2004/12/27/1334.aspx這是很老所以有可能做更好的方法現在做同樣的事 – OldBoyCoder