How do you detect when a user clicks on an image on a page being displayed by a WebBrowser Control (VB.Net)?

I use a WebBrowser control to display a series of images.

I'd like to be able to determine when the user clicks on one of the images.

I'm handling the WebBrowser document's Click event but can't figure out how to determine what the underlying html element was clicked.

Here's what the WebBrowser control is displaying:

<img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030000-W.jpg" width=180 height=140 title="CA030000-W.jpg" />
<img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030001-W.jpg" width=180 height=140 title="CA030001-W.jpg" />
<img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030002-W.jpg" width=180 height=140 title="CA030002-W.jpg" />

Open in new window

Here's my Click handler. It captures the click event but the code below always detects the <Body> element and not an individual <IMG> element.

Private Sub htmlDoc_Click(sender As Object, e As System.Windows.Forms.HtmlElementEventArgs) Handles htmlDoc.Click

' browser is the WebBrowser control


		Dim BrowserCoord As Point = browser.PointToClient(e.MousePosition)
		Dim ele As HtmlElement = browser.Document.GetElementFromPoint(BrowserCoord) 'htmlDoc.ActiveElement

	Catch ex As Exception

	End Try

End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Well, it really depends on you use case. I'm using in most cases custom anchors:

		<a href="img:1"><img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030000-W.jpg" width=180 height=140 title="CA030000-W.jpg" /></a>
		<a href="img:2"><img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030001-W.jpg" width=180 height=140 title="CA030001-W.jpg" /></a>
		<a href="img:3"><img src="C:\Users\John\Documents\jwLACScraper\CA03\CA030002-W.jpg" width=180 height=140 title="CA030002-W.jpg" /></a>

Open in new window

and capture the click in before navigate and cancle the navigate event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can handle the Click event of the images:

        For Each image As HtmlElement In htmlDoc.Images
            AddHandler image.Click, AddressOf image_Click

Open in new window

ou81aswellAuthor Commented:
I ended up embedding the images within an anchor similar to ste5an and then handling it inside the WebBrowser control's Navigating event.

louisfr's approach of adding a click handler to each image would have also worked for me.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.