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:

<html><head><title></title></head>
<body>
<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" />
</body>
</html>

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

	Try

		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

ou81aswellAsked:
Who is Participating?
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:

<html>
	<body>
		<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>
	</body>
</html>

Open in new window


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

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
louisfrCommented:
You can handle the Click event of the images:

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

Open in new window

0
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.

Thanks.
0
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.

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.