views:

451

answers:

2

I've googled it, but came out empty. And the worst thing is that I know it is possible.

Anyway, I'm developing an application that uses the WebBrowser control to display information regarding an object (like Outlook does with the Rules and Alerts dialog box).

My question is how do I do for the click on a, say, hyperlink in the WebBrowser execute some function within the Windows Form?

For instance, say I have a link like this and when I click it I want the application to display an specific form, like the Outlook does when you click on hyperlinks like People and Distribution List

+7  A: 

This looks useful: How to: Implement Two-Way Communication Between DHTML Code and Client Application Code

ChrisW
That's what I like about stackoverflow. Ask and shall recieve. :P
Paulo Santos
+5  A: 

ChrisW's answer will work, but there's another way if you're just relying on hyperlinks.

In Comicster, I have links in my WebBrowser control like this:

<a href="action:FileNew">New Collection</a>

And then in the WebBrowser's Navigating event, I have some code to check if the user has tried to navigate to an "action:" link, and intercept it:

private void webBrowser1_Navigating(object sender,
    WebBrowserNavigatingEventArgs e)
{
    if (e.Url.Scheme == "action")
    {
        e.Cancel = true;

        string actionName = e.Url.LocalPath;
        // do stuff when actionName == "FileNew" etc
    }
}

With a little bit of code you can even parse the URL parameters and "pass them through" to your host application's action, so I can do things like:

<a href="action:EditIssue?ID=1">Edit this issue</a>

... which will open a properties dialog for the issue with ID 1.

Matt Hamilton
Is this way preferable for any reason?
ChrisW
I use this method because I have end-users who know basic HTML but don't know javascript, and I let them create their own 'skins". This way they only need to know the action names for their links.
Matt Hamilton

related questions