[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MSHTML mouseover events to identify html elements

Posted on 2006-05-30
10
Medium Priority
?
1,287 Views
Last Modified: 2008-01-09
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
Comment
Question by:famoso
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16789466
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
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16789487
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
 

Author Comment

by:famoso
ID: 16790902
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16791095
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
 

Author Comment

by:famoso
ID: 16791147
Understood.  Thank you for your assistance.
0
 

Author Comment

by:famoso
ID: 16792572
Please refund points.
0
 

Author Comment

by:famoso
ID: 16919297
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
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17101383
PAQed with points refunded (250)

DarthMod
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question