Question Which HTML Element is clicked in WEBBROWSER CONTROL

sonia.sardana

Active member
Joined
Jan 25, 2009
Messages
35
Programming Experience
Beginner
WHen we click on site whether left click or right....Control goes to IEDoc_MouseDown event,I just want to ask is that possible to know taht which element is clicked..Suppose we right click on link,is that possible to know it that link is clicked.

VB.NET:
Public Class Form2
    Dim WithEvents IEDoc As System.Windows.Forms.HtmlDocument
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        WebBrowser1.Navigate("www.google.com")
        If WebBrowser1.IsWebBrowserContextMenuEnabled Then
            WebBrowser1.IsWebBrowserContextMenuEnabled = False
        End If
    End Sub

    Private Sub WEB_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Try
            IEDoc = WebBrowser1.Document
            
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub IEDoc_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles IEDoc.MouseDown
        If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Right Then
            MsgBox("sonia")
        End If
    End Sub

End Class

PLZ let me know...Very Urgent
 
VB.NET:
Dim el As HtmlElement = CType(sender, HtmlDocument).GetElementFromPoint(e.ClientMousePosition)
 
Sir Mine Complete code is as below-

VB.NET:
Imports mshtml
Public Class Form1
   
    Dim WithEvents IEDoc As System.Windows.Forms.HtmlDocument
    Dim IECollection As System.Windows.Forms.HtmlElementCollection
    Dim IELink As System.Windows.Forms.HtmlElement
    Dim handler As HtmlElementEventHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com")
 End Sub


 Private Sub WEB_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Try
           
            IEDoc = WEB.Document
            IECollection = IEDoc.Links
                   Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub IEDoc_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles IEDoc.MouseDown
        Try

            If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Right Then
                Dim el As HtmlElement = CType(sender, HTMLDocument).GetElementFromPoint(e.ClientMousePosition)
End If
End Sub

Sir Error is there in line Dim el As HtmlElement = CType(sender, HTMLDocument).GetElementFromPoint(e.ClientMousePosition)


Unable to cast object of type 'System.Windows.Forms.HtmlDocument' to type 'mshtml.HTMLDocument'
 
when we right click suppose on images link on google in webbrowser i want to just know that whether the we click on hyperlink or not....

Sir the other site Solution-
Hi Sonia,

I Suggest using the ContextMenuShowing instead of MouseDown , as it seems that MouseDown is 1 click behind and don't give the current element but it gives the previous element that rightclick is occur on it.

Now, to get the element (I don't know what information you want) info, just put a debug point as I post in my example below and search under Sender.ActiveElement
Replace the mouse down event with this.

VB.NET:
      Sub IEDoc_ContextMenuShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles IEDoc.ContextMenuShowing
        Dim ElementInfo = sender.ActiveElement.ID + vbTab + sender.ActiveElement.InnerHTML ' <-- Put debug point here and add sender to the watch window and search under activeelement
       Debug.Print(ElementInfo)
      End Sub

But its also not working!!!!!
 
Sir mine code that is working in post 4 is working... there is no edit option in this site,dats y i cant edit it..


Hey frnd i have test this code on many sites...WHen we click on any hypelink
then in ElementInfo "A" is coming.....

Sub IEDoc_ContextMenuShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles IEDoc.ContextMenuShowing

Dim ElementInfo As String = sender.ActiveElement.tagname
MsgBox(ElementInfo)

End Sub

Can u plz tell me i m gng in d right way.....
 
Unable to cast object of type 'System.Windows.Forms.HtmlDocument' to type 'mshtml.HTMLDocument'
You seem to have mshtml namespace imported, but not System.Windows.Forms namespace? Mshtml has no place in this code.
 
Back
Top