vs 2019 vb.net .vb How to capture a MouseUp event in WebBrowser1.

vmars316 621 Reputation points
2020-09-12T21:12:33.42+00:00

Hello & Thanks ;
I am trying to capture a MouseUp event in WebBrowser1. .
But the two codes I have don't work :
Pls , what am I doing wrong ?
Thanks for your Help...
Code1:

     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
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,860 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-09-14T02:48:28.307+00:00

    Hi Vernon Marsden,
    First, the WebBrowser control does not have a mouse up event. Because the mouse event occurs in the displayed document, not the control itself.
    So you need to capture mouse events associated with the document object in the webBrowser control.
    Here is my test code you can refer to.

     Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            AddHandler WebBrowser1.Document.MouseUp, AddressOf eventSub  
        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)  
        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  
        End Sub  
        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>"  
        End Sub  
    

    The result:
    24210-914.gif
    >>How do I "Add reference to Microsoft HTML Object Library" ?
    You can add it by following the steps below:
    Right click your project ->Add-> Reference->Search for Microsoft HTML and choose Microsoft.mshtml->OK
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Castorix31 82,226 Reputation points
    2020-09-13T09:37:09.627+00:00

    This works for me , with a webBrowser1 control :

    ' Add reference to Microsoft HTML Object Library
    

    -
    AddHandler webBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf webBrowser1_DocumentCompleted)

    -
    Dim bHandlerOK = False
    Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim doc As mshtml.HTMLDocument
    doc = webBrowser1.Document.DomDocument
    If (Not bHandlerOK) Then
    AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onmouseup, AddressOf Document_onmouseup
    bHandlerOK = True
    End If
    End Sub

    Private Function Document_onmouseup(ByVal e As mshtml.IHTMLEventObj) As Boolean
        Console.Beep(5000, 10)
        Console.WriteLine("onMouseUp: " & e.srcElement.tagName.ToString())
        Return True
    End Function
    
    0 comments No comments

  2. vmars316 621 Reputation points
    2020-09-13T14:42:56.507+00:00

    Thanks ;
    I get the following errors'
    1>C:\Users\vmars\source\repos\GetElementByTag\Form1.vb(24,52): error BC30002: Type 'mshtml.IHTMLEventObj' is not defined.
    1>C:\Users\vmars\source\repos\GetElementByTag\Form1.vb(15,17): error BC30269: 'Private Sub webBrowser1_DocumentCompleted(sender As Object, e As
    24) is this- Private Function Document_onmouseup(ByVal e As mshtml.IHTMLEventObj) As Boolean
    15) is this- Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)

    I don't underrstand why DocumentCompleted is coded ?
    If user clicks on a certain image-link , a new instance of IE is opened .
    Will that cause a DocumentCompleted event ?
    Also , is this in the proper place- AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf WebBrowser1_DocumentCompleted)
    Thanks
    Private Sub GetElementsByTagName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    WebBrowser1.ScriptErrorsSuppressed = True
    WebBrowser1.IsWebBrowserContextMenuEnabled = False
    ' Me.MinimumSize = New Size(1000, 650)
    Me.StartPosition = FormStartPosition.CenterScreen
    Me.Left = 40
    Me.Top = 20
    WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf WebBrowser1_DocumentCompleted)
    End Sub
    Dim bHandlerOK = False
    Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim doc As mshtml.HTMLDocument
    doc = WebBrowser1.Document.DomDocument
    If (Not bHandlerOK) Then
    AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onmouseup, AddressOf Document_onmouseup
    bHandlerOK = True
    End If
    End Sub

        Private Function Document_onmouseup(ByVal e As mshtml.IHTMLEventObj) As Boolean
            Console.Beep(5000, 10)
            Console.WriteLine("onMouseUp: " & e.srcElement.tagName.ToString())
            Return True
        End Function
    

  3. vmars316 621 Reputation points
    2020-09-14T15:19:50.39+00:00

    Thanks Daniel ;
    I would like to try out your Answer .
    But my code is getting very messy .
    I have asked this question before , but got no answer .
    How can I clone my project under a new name ?
    Thank you !

    0 comments No comments

  4. vmars316 621 Reputation points
    2020-09-14T15:32:12.317+00:00

    Daniel ;
    Also
    [ You can add it by following the steps below:
    Right click your project ->Add-> Reference->Search for Microsoft HTML and choose Microsoft.mshtml->OK ]

    What do you mean by project ?
    When I click on Project Tab , gazillions of options pop up .
    Thanks