Hail Hydra
Hail Hydra

Reputation: 473

Click html button and enable button in Visual Basic

I'm new with Visual Basic and I have a simple app in visual basic using the WebBrowser component, the web page that is loaded has a button (html) and I would like to enable a button of visual basic when press that html button, I have no idea how to achieve this, I read that I can do this but not how, I searched here in SO and youtube but I didn't found what I'm looking for, can you help me to achieve this please?, thanks.

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

Upvotes: 0

Views: 3982

Answers (1)

AwatITWork
AwatITWork

Reputation: 586

I can simplify that for you :), and its very easy.

What I am using is the following: 1- HTML page "Index.html" with one simple javascript function called "CheckStat" to change your buttons value. 2- Form with 1 Button, 1 WebBrowser and 1 Timer.

"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> 

This is what I did from Visual Studio, first this is my code: "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

Because you said i am new for VB.net, let me tell you what is going on. when you click on Button1 the WebBrowser will load that file for you, and also enables Timer1.

now browser loaded the page and timer1 run that code every 100 intervals 1 Sec = 1000 interval, first it searches for all Element from HTML page which its ID equals to "login" and it returns to the variable called "loginbtn".

then it checks for its value (The text or caption that appears on HTML button) if it's equal to "clicked" then it means user click the HTML button.

Then it disables Button (VB's Button), changes HTML'S Button value to "click me again" and shows message box...

For any problem, it shows this message box to the user and tells him/her what was wrong....

Yes you can do it, but i used timer because it check it every i.e 1 min or 1 hour or any time you set...I updated for you and i created this function please let me know if you don't understand it or anything else.... you can disable or remove your timer, add one button and add this simple code to it which i put it in Button2... one more thing, if you see i create my function as a Private, so you can't called from any other form, or class, etc. you can change it to Public so you can use it anywhere in your app. How do i call it then? . like this : Form1.CheckStatByid("login"). updated code is :

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

Hope you understand it. and I am sorry for any grammar or spelling error. ...

Upvotes: 1

Related Questions