Answered How to Capture a click on WebBrowser1 Event ?

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Hello & Thanks ;
vs vb.net 2019
On some sites (ie. this one kidzsearch.com) a click on an image-link initiates a newWindow or newTab IE instance. Looking at the .html , its not a normal link , there is no (target)="blank" , & I can't figure how they are opening a new instance of IE .
So what I want to do is to catch the click on WebBrowser1 control , and then Close or KIll that new instance.
I know how to do Close/Kill , but I don't know how to catch a WebBrowser1 click . Either left or right click will do .
I have read all the ms.Docs , but I can't turn that info into actual code .
Thanks for your Help...
 
Btw: Here are some of the codes I have tried:
VB.NET:
      Private Sub WebBrowser1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    
          MsgBox("We are Here : Sub WebBrowser1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)")
    
      End Sub

Code2:

 Private Sub WebBrowser1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
 If e.Button = MouseButtons.Left Then
    
 MsgBox("Form1_MouseUp(ByVal sender As System.Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown")
    
 End If
 End Sub
The MsgBox'es' never show .
 
VB.NET:
    Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        If Not e.Url.ToString.Contains("microsoft.com") And Not e.Url.ToString.Contains("about:blank") Then
            e.Cancel = True
        End If

    End Sub

Now you have a new problem. You need to prevent right click on the links and disable the menu so they don't open it in a new instance of IE.
 
Now you have a new problem. You need to prevent right click on the links and disable the menu so they don't open it in a new instance of IE.
Thanks ;
I handle that in the program Load with this-
WebBrowser1.IsWebBrowserContextMenuEnabled = False

Once I do below , is there any way to also know what is address of the link clicked , or has that Event already gone by ?
Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
If Not e.Url.ToString.Contains("microsoft.com") And Not e.Url.ToString.Contains("about:blank") Then
e.Cancel = True
End If

End Sub

Thanks
 
Last edited by a moderator:
Thanks ;
[
is there any way to also know what is address of the link clicked , or has that Event already gone by ?
I already showed you what link was clicked. You can read it in the link being navigated too. Try what I gave you.
]

But doesnt the MouseUp event happen before the Navigating event ?
Thanks
 
Step into the code with your debugger and place break points on the first line of each and you will find out when you click a link and see which shall fire first...
 
Why would it mater if the mouse up event fires or not. What I gave you stops navigating if it's a URI you don't like.

You only need to change the logic of the if statement to meet whatever conditions you want. Example :
VB.NET:
    Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        If e.Url.ToString.Contains("about:blank") Or e.Url.ToString.Contains("xbox.com") Then
            e.Cancel = True
        End If
    End Sub
If the Uri is about:blank or xBox.com it will cancel the events of navigating as e.Cancel is changed to true. If you have a list of domains you don't want to access, then check the Uri against a list of bad website addresses.
 
Thanks ,
Yes , you are correct , both navigating and MouseUp run .
But I would like to know , why does
Private Sub CloseIExplore()
and WebBrowser1_DocumentCompleted
Run so many times i a row ?
Thanks for your Help..
VB.NET:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '        WebBrowser1.DocumentText =
        '    "<html><body>Please enter your name:<br/>" &
        '    "<input type='text' name='userName'/><br/>" &
        '    "<a href='http://www.microsoft.com'>continue</a>" &
        '    "</body></html>"
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        '        Me.MinimumSize = New Size(1000, 650)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Left = 40
        Me.Top = 20
        WebBrowser1.Navigate("https://www.kidzsearch.com/")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandler WebBrowser1.Document.MouseUp, AddressOf eventSub
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "Private Sub WebBrowser1_DocumentCompleted"
    End Sub
    Sub eventSub(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        Dim event_html As New HtmlElementEventHandler(AddressOf webMouseUp)
        event_html.Invoke(sender, e)
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "Sub eventSub"
    End Sub
    Sub webMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then
            '            MsgBox("left clicked!")
        End If
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "Sub webMouseUp"
        CloseIExplore()
    End Sub
    Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        '        If e.Url.ToString.Contains("about:blank") Or e.Url.ToString.Contains("xbox.com") Then
        '            e.Cancel = True
        '            End If
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & e.Url.ToString

    End Sub
    Private Sub CloseIExplore()
        Dim processArray As Process() = Process.GetProcessesByName("iexplore")
        For Each p As Process In processArray
            p.Close()
            TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "For Each p As Process"
        Next
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "Private Sub CloseIExplore()"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        CloseIExplore()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "----"
        TextBox1.Text = TextBox1.Text & ControlChars.NewLine & "----"
    End Sub
End Class
Do you need more from me to be able to run code and see what I mean ?
Thanks
 
Last edited:
Back
Top