• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1396
  • Last Modified:

MSHTML mouseover events to identify html elements

I need code - C# preferrablly.

I have a WinForm, Browser Control and TextBox.  I navigate to http://www.google.com/ when the form loads.

I would like the elements on the page to appear in the Textbox as I move the mouse over the webpage.

Examples:
As I move the mouse over the textbox on the Google page, in my WinForm Textbox should be something like:
Input

As I move the mouse over one of the buttons on the Google page, in my WinForm Textbox should be something like:
(Bad example but ... )  Input  (I could get further if I had code to get me that far)

I would collect more info as in the case of the Button ie: Input - type=submit, etc.

I just need a nudge in the right direction.  Anybody???
0
famoso
Asked:
famoso
  • 4
  • 3
1 Solution
 
AndyAelbrechtCommented:
OK, as we are in the VB.NET section of the website, this shouldn't be here.

now that is out of the way, here's a possible solution to your problem:

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.be");
        }

        void thisdoc_MouseMove(object sender, HtmlElementEventArgs e)
        {
            textBox1.Text = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).TagName;
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            HtmlDocument thisdoc;
            thisdoc = webBrowser1.Document;
            thisdoc.MouseMove += new HtmlElementEventHandler(thisdoc_MouseMove);
        }

explanation: the WebBrowser has no MouseMove event handler, but the underlying document does; we only need to add an eventhandler to the current document every time we navigate in the webbrowser; this event handler has 1 line of code: it will get the element(frompoint)'s tagname and put it in your textbox.

if you want to see other properties of the elements you are hovering, feel free to replace the TagName with something else (just remove it and let IntelliSense guide you).

I hope this helps,

cheers,
Andy
0
 
AndyAelbrechtCommented:
I was almost forgetting that you can't just add this code to your project ofcourse :-)
But it is pretty obvious what events are firing the eventhandlers you see here.

just in case:
Form1_Load -> Form Load event
webBrowser1_Navigated -> webBrowser(1) Navigated

thisdoc_MouseMove -> this can be added as is (this eventhandler is coupled to the thisdoc.MouseMove event every time we navigate to a new page)
0
 
famosoAuthor Commented:
I appologize but I had asked this question days ago and I've been having problems having it listed on here.  I got frantic and placed it here too assuming that I would get a VB answer but if C# was available that would be even better.

So far this is what I have:  The problem is that I can't get axWebBrowserq.Document to have a GetElementFormPoint() method.  I only get: Equals, GetHashCode, GetType, and ToString

Also was I right to add System.Windows.Forms to the HtmlDocument?

    void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) {
      System.Windows.Forms.HtmlDocument thisdoc;
      thisdoc=(System.Windows.Forms.HtmlDocument)axWebBrowser1.Document;
      thisdoc.MouseMove+=new HtmlElementEventHandler(thisdoc_MouseMove);
    }

    void thisdoc_MouseMove(object sender, HtmlElementEventArgs e) {
      txtPageInfo.AppendText(axWebBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).TagName);
    }
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AndyAelbrechtCommented:
hmmm
My code assumes Visual Studio 2005 actually, which uses a WebBrowser component and not an ActiveX Internet Explorer component.

If you have VS2005, I highly suggest using the WebBrowser component.

If you don't, I'm afraid somebody will have to help you further as I don't have VS2003 (mandatory upgrades :()

cheers,
Andy
0
 
famosoAuthor Commented:
Understood.  Thank you for your assistance.
0
 
famosoAuthor Commented:
Please refund points.
0
 
famosoAuthor Commented:
I got by with a little help from my friends.

                Dim doc As mshtml.HTMLDocument
                doc = wbrETL.Document
                If klicked = False Then
                    klicked = True
                    Try
                        //  Also, there is an onMouseOver() from HTMLDocumentEvents2_Events.
                        AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf Document_onclick
                    Catch ex As Exception
                        MessageBox.Show("AddHandler Failed: " + ex.Message)
                    End Try
                Else
                    klicked = False
                    Try
                        RemoveHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf Document_onclick
                    Catch ex As Exception
                        'No need for an Error Handler here [famoso]
                    End Try
                End If

    Private Function Document_onclick(ByVal e As mshtml.IHTMLEventObj) As Boolean
        lstEventLog.Items.Insert(0, "onClick: " & e.srcElement.outerHTML.ToString())
        Return False
    End Function
0
 
DarthModCommented:
PAQed with points refunded (250)

DarthMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now