2017-04-06 66 views
0

我是新的Visual Basic,我必须使用Visual Basic中的WebBrowser组件,加载的网页上有一个按钮(HTML)的简单应用程序,我想启用一个可视化基本按钮时,按下该HTML按钮,我不知道如何实现这一点,我读我可以做到这一点,但不是如何,我在SO和YouTube搜索这里,但我没有找到我在找什么因为,你能帮我实现这个吗?,谢谢。点击HTML按钮,启动按钮在Visual Basic

<button type="button" id="login">Login</button>

+0

这不是一个Visual Studio的问题。 – EJoshuaS

+0

对不起我没有时间将其转换为VB,但这里是一个C#解决这个问题:http://ryanfarley.com/blog/archive/2004/12/27/1334.aspx这是很老所以有可能做更好的方法现在做同样的事 – OldBoyCoder

回答

1

我可以简化为你:),它很容易。

我使用的是以下内容: 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 

希望你能理解它。我很抱歉有任何语法或拼写错误。 ...

+0

没有计时器,我可以做到吗? –

+0

是的,请让我知道如果它清楚,并且你的问题已经解决或没有... – AwatITWork

+0

是的,它为我工作,谢谢 –