0

VB.NET rookie here,

Is there any way to handle an HTML object being clicked inside of a web browser control? The closest I've managed to get is with a mouse-down event handler for the browser which writes the position of the mouse, but I have no way getting information about what html object was clicked.

Private Sub myWB_mouseDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then
        'mousedown event
        Debug.WriteLine(e.ClientMousePosition)
    End If
End Sub
2
  • Parse the Events of the HtmlElement object. HtmlDocument.Body is one HtmlElement (it can be tricky, though). Update the question when you find the next (apparently) odd behaviour. Commented Aug 8, 2018 at 23:20
  • If you have any further questions or issues regarding this or my implementation, just let me know in a comment on my answer! Commented Aug 9, 2018 at 11:03

1 Answer 1

1

Since you have the coordinates of the click you can use HtmlDocument.GetElementFromPoint() to get the element at the point of the click.

You can then check the element's TagName property to determine the type of the element that was clicked, namely if it's a <button> or an <input> element. If the latter, you also need to check GetAttribute("type") to determine if the type of the input element is submit, which means it is a button.

If myWB.Document IsNot Nothing AndAlso e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then
    Dim ClickedElement As HtmlElement = myWB.Document.GetElementFromPoint(e.ClientMousePosition)

    If ClickedElement IsNot Nothing AndAlso _
        (ClickedElement.TagName.Equals("button", StringComparison.OrdinalIgnoreCase) OrElse _
         (ClickedElement.TagName.Equals("input", StringComparison.OrdinalIgnoreCase) AndAlso ClickedElement.GetAttribute("type").Equals("submit", StringComparison.OrdinalIgnoreCase))) Then

        'ClickedElement was either a <button> or an <input type="submit">. Do something...

    End If
End If
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.