Link to home
Start Free TrialLog in
Avatar of Teknosoft
Teknosoft

asked on

Webbrowser Control click event trapping

Hello Experts,

I am developing an application that will utilize the webbrowser control. (VB.NET 2005) I need to trap user interactions within the webbrowser to trigger an event.

I was able to trap link clicking within the webbrwoser but I am unable to trap button clicks and/or image button click to an event.

This is important for me to be able to know when the user clicks such buttons that I can read informations entered into the for prior to submittion.

I am not sure if this is possible or not please let me know what you think.

Thank you


'This is a code to trap the link clicking event hope it will help to get the button clicking event.
 
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
 
        Dim olink As HtmlElement
        Dim olinks As HtmlElementCollection = WebBrowser1.Document.Links
 
        For Each olink In olinks
            olink.AttachEventHandler("onclick", AddressOf LinkClicked)
        Next
 
    End Sub
 
 
 
    Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim link As HtmlElement = WebBrowser1.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")
 
        MsgBox("Link Clicked: " & link.InnerText & vbCrLf & _
                    "Destination: " & url)
    End Sub

Open in new window

Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

you can use e.URL in document complete event or you can use htmldocument object to check page for specific text etc... I can be more specific if you tell exactly what you are trying to do.
There are two three things in it.

1. A button or link can open a new window which cna have any url and you can trap that url
by following method

Make a class named ExtendedWebBrowser.vb and paste the code below in it..
then use ExtendedWebBrowser instead of WebBrowser and you will have a new event in it, called
NewWindowWithTaget
which has WebBrowserExtendedNavigatingEventArgs as args, you can get the target url from it

Now when ever a new window is going to open

Imports System.Runtime
Imports System.ComponentModel
 
 
 
'Extend the WebBrowser control 
Public Class ExtendedWebBrowser
    Inherits WebBrowser
    Private cookie As AxHost.ConnectionPointCookie
    Private events As WebBrowserExtendedEvents
 
    'This method will be called to give you a chance to create your own event sink 
    Protected Overloads Overrides Sub CreateSink()
        'MAKE SURE TO CALL THE BASE or the normal events won't fire 
        MyBase.CreateSink()
        events = New WebBrowserExtendedEvents(Me)
        cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
    End Sub
 
    Protected Overloads Overrides Sub DetachSink()
        If cookie IsNot Nothing Then
            cookie.Disconnect()
            cookie = Nothing
        End If
        MyBase.DetachSink()
    End Sub
 
    'This new event will fire when the page is navigating 
    Public Event NewWindowWithTaget As EventHandler(Of WebBrowserExtendedNavigatingEventArgs)
    Protected Sub OnNewWindow3(ByVal url As String, ByVal e As WebBrowserExtendedNavigatingEventArgs)
        RaiseEvent NewWindowWithTaget(Me, e)
    End Sub
    'This class will capture events from the WebBrowser 
    Private Class WebBrowserExtendedEvents
        Inherits System.Runtime.InteropServices.StandardOleMarshalObject
        Implements DWebBrowserEvents2
 
        Private _Browser As ExtendedWebBrowser
        Public Sub New(ByVal browser As ExtendedWebBrowser)
            _Browser = browser
        End Sub
 
        Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object) Implements DWebBrowserEvents2.NewWindow3
            Dim args As New WebBrowserExtendedNavigatingEventArgs(URL)
            args.Cancel = cancel
            _Browser.OnNewWindow3(URL, args)
            cancel = args.Cancel
        End Sub
    End Class
    <InteropServices.ComImport(), InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InteropServices.InterfaceTypeAttribute(InteropServices.ComInterfaceType.InterfaceIsIDispatch), InteropServices.TypeLibType(InteropServices.TypeLibTypeFlags.FHidden)> _
    Public Interface DWebBrowserEvents2
 
        <InteropServices.DispId(273)> _
        Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object)
    End Interface
End Class
 
Public Class WebBrowserExtendedNavigatingEventArgs
    Inherits CancelEventArgs
    Private _Url As String
    Public Sub New(ByVal url As String)
        _Url = url
    End Sub
    Public ReadOnly Property Url() As String
        Get
            Return _Url
        End Get
    End Property
End Class

Open in new window

2. you can also handle document_click event to get the element clicked.



Avatar of Teknosoft
Teknosoft

ASKER

armoghan,

This class will capture the clic event when the link/button is trying to open a new window.
What I am trying to do;

for example going to google.com and the user enter some text in the search criteria when they click on the search button I want an event fired to allow me run few procedures to read the test in the filed prior to the for submission.

My actual project is to capture user entry prior to submittion the form.
try my second comment,

when document is successfully loaded. try attaching a click handler on it,
armoghan, cna you tell me how to attach a click handler to it, as this is what I was looking for.

sorry for not replying back early, I was traveling away.
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solution was what I was looking for, thank you for the detail information.
Thank you for this detailed solution, I was able to get what I was looking for, thanks again
would I be able to use this solution to intercept a button click on a webage form and stop the button's code from being executed?  for example the button I want to intercept is a <input type="file">  button that loads an open file dialog box.  I'd like to intercept the button click, stop the dialog from showing, and replace the dialog box with a msgbox.