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

TeknosoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cem TürkSenior Software EngineerCommented:
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.
0
armoghanCommented:
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

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



0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

TeknosoftAuthor Commented:
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.
0
armoghanCommented:
try my second comment,

when document is successfully loaded. try attaching a click handler on it,
0
TeknosoftAuthor Commented:
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.
0
armoghanCommented:
I think this code should give you click and should also give you focused element.
I have just pasted chunks of a larger application, so you may have to tweek a little bit to get it to work
' Private variable to get document of when document completely loads.
Private WithEvents _document As HtmlDocument
Private oldDocument As mshtml.IHTMLDocument
 
' Document Complete event of browser.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
  _document = Me.WebBrowser1.Document
oldDocument = _document.DomDocument
End Sub
 
' Doumnet  click event 
Private Sub _document_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles _document.Click
Dim focusedElement As mshtml.IHTMLElement = GetFocusedElement()
End Sub
 
'''''Additional helper funcations 
'''To get Focused element
 Public Function GetFocusedElement() As mshtml.IHTMLElement
        Dim focusedElement As mshtml.IHTMLElement = Nothing
        If oldDocument.selection.type = "None" Then
            focusedElement = CType(oldDocument.selection.createRange(), mshtml.IHTMLTxtRange).parentElement
        ElseIf oldDocument.selection.type = "Control" Then
            focusedElement = CType(oldDocument.selection.createRange(), mshtml.IHTMLControlRange).item(0)
        End If
 
        Return focusedElement
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TeknosoftAuthor Commented:
This solution was what I was looking for, thank you for the detail information.
0
TeknosoftAuthor Commented:
Thank you for this detailed solution, I was able to get what I was looking for, thanks again
0
Daveb7945Commented:
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.