famoso
asked on
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???
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???
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)
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)
ASKER
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_NavigateComp lete2(obje ct sender, AxSHDocVw.DWebBrowserEvent s2_Navigat eComplete2 Event e) {
System.Windows.Forms.HtmlD ocument thisdoc;
thisdoc=(System.Windows.Fo rms.HtmlDo cument)axW ebBrowser1 .Document;
thisdoc.MouseMove+=new HtmlElementEventHandler(th isdoc_Mous eMove);
}
void thisdoc_MouseMove(object sender, HtmlElementEventArgs e) {
txtPageInfo.AppendText(axW ebBrowser1 .Document. GetElement FromPoint( e.ClientMo usePositio n).TagName );
}
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_NavigateComp
System.Windows.Forms.HtmlD
thisdoc=(System.Windows.Fo
thisdoc.MouseMove+=new HtmlElementEventHandler(th
}
void thisdoc_MouseMove(object sender, HtmlElementEventArgs e) {
txtPageInfo.AppendText(axW
}
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
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
ASKER
Understood. Thank you for your assistance.
ASKER
Please refund points.
ASKER
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).on click, AddressOf Document_onclick
Catch ex As Exception
MessageBox.Show("AddHandle r Failed: " + ex.Message)
End Try
Else
klicked = False
Try
RemoveHandler CType(doc, mshtml.HTMLDocumentEvents2 _Event).on click, 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.ToS tring())
Return False
End Function
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
Catch ex As Exception
MessageBox.Show("AddHandle
End Try
Else
klicked = False
Try
RemoveHandler CType(doc, mshtml.HTMLDocumentEvents2
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
Return False
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.GetEl
}
private void webBrowser1_Navigated(obje
{
HtmlDocument thisdoc;
thisdoc = webBrowser1.Document;
thisdoc.MouseMove += new HtmlElementEventHandler(th
}
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