Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a VB.NET forms application that uses the webcontrol to show a local html page with music album covers. All covers contain a link to a local text file. I added a handler to the linkcollection, so that when clicking on a cover, a subroutine reads the text file and adds its content to a music player. This works flawlessly.

However, I am now converting this application to WPF, and I have been trying to add a handler to the WPF webcontrol for days now, but just don't get it to work.

Googling only gives me C++ examples that I am only able to convert partially to VB.NET.


The code I have so far:


VB
Private Sub WhatsNewBrowser_LoadCompleted(sender As Object, e As NavigationEventArgs) Handles WhatsNewBrowser.LoadCompleted
        Dim doc As mshtml.HTMLDocumentClass = WhatsNewBrowser.Document 

        For Each link As mshtml.IHTMLElement In doc.links
            Dim anchor As mshtml.HTMLAnchorElement = link
            If Not anchor Is Nothing Then
                Dim handler As mshtml.HTMLAnchorEvents_Event = anchor
                If Not handler Is Nothing Then
                    ''what code goes here to run a sub onclick? The sub     
                    ''obviously needs to know what link is clicked.
                End If
            End If
        Next

    End Sub


This code does not work and I have no idea what to change.
What am I doing wrong and how do I create the handler to run the sub.

Thanks in advance!
Posted
Updated 16-Nov-14 8:49am
v2

1 solution

A handler is created as follows:
VB
AddHandler eventargs, AddressOf subroutine
. I.e. AddHandler TextBox1.KeyDown, AddressOf MySub

Of course, you need to have a subroutine called MySub for this example, and from here you add the code you want to do the work for that event to that subroutine (MySub). See Adding Handlers.

How to Pass Values to Subroutines will demonstrate how you can send values to other subroutines from your Link declaration in your For Each Loop.

Your For Loop belongs in the Navigating EventArgs subroutine.

From there, you can check the URL that's been navigated to. (Obviously you would only be navigating if you click a link, so this is the correct sub to use).

Because the WebBrowser control raises the Navigating event when it's about to navigate to a new page and it will fire the Navigated event when it has started navigating to a new page.

Using these events will allow you to check the link that was clicked in your loop, against where you are navigating to. However, by using the navigating and navigated event args, you don't need a loop.

Hope this helps.
 
Share this answer
 
v3
Comments
fanoftheplanet 18-Nov-14 10:15am    
Navigating is called whenever the WebBrowser starts navigating (hence the name). This happens not only when a link is clicked but also e.g. when initialising the WebBrowser control. So it's not exactly the same.

However, in my case, using the navigating event as you said, in my case does the job, because I just look at the URL and filter out the links I need. Thanks!
[no name] 18-Nov-14 12:55pm    
Great, glad to help! Good to hear it worked out for you.
fanoftheplanet 18-Nov-14 13:28pm    
Maybe you can help with one more thing: I do not only need the clicked link, but also the path of the clicked element.
E.g. I have an image with a link. When I click on the image I need to know the uri of the clicked image as well. Is this possible?

(Sender gives the the webbrowser handle, not the clicked element).
[no name] 18-Nov-14 13:56pm    
This a different issue for a different topic. You should create a new question for this.

However, to answer you; while looping or navigating you can iterate through the pages links; while doing so, you check If the ' a ' tag has a sub tag of ' img ' and if it does, then you have the pictures filename and location including the hyperlink.

If you ask a new question, I will answer it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900