How to open a link in webBrowser control in extern

2020-02-10 02:28发布

问题:

I have a textBox and a webBrowser control in my Windows Forms application. Whenever a user enters a HTML code in textBox, the webBrowser control shows its compiled form. The code for this:

private void textBox2_TextChanged(object sender, EventArgs e)
{
    webBrowser1.DocumentText = textBox2.Text;
}

But whenever I click a link in the webBrowser control, it opens it in the same webBrowser control. What I want is that it should open in default web browser of the system. So is there any event for this webBrowser control that handles link clicking?

回答1:

The easiest way to do this would be to intercept the Navigating event.

public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    //cancel the current event
    e.Cancel = true;

    //this opens the URL in the user's default browser
    Process.Start(e.Url.ToString());
}


回答2:

I would like to add something more to this answer,

Coz webBrowser1_Navigating method is executed everytime when the content of the webBrowser is changed.

In your case whenever you set the values to DocumentText this method is called and when there is no url and its default value is about:blank. So we should also check this for otherwise it won't load any content.

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (!(e.Url.ToString().Equals("about:blank", StringComparison.InvariantCultureIgnoreCase)))
        {
            System.Diagnostics.Process.Start(e.Url.ToString());
            e.Cancel = true;
        }
    }


回答3:

This code here should work:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    System.Diagnostics.Process.Start(e.Url.ToString());

    e.Cancel = true;
}

I tried this to be sure it worked and it does!

Hope this helps!!



回答4:

Process.Start will open the URL in the default browser, and then you just tell the WebBrowser control to cancel navigation.

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    Process.Start(e.Url.ToString());

    e.Cancel = true;
}

I just created a sample app to test it - it worked.



回答5:

Perhaps you could try using Process.Start() on the Navigating event?



回答6:

Because no one has made a decent answer to links with target="_blank" in them. I will attempt to do so.

First, why doesn't this work? Because when a user clicks on a link in your web browser control, it doesn't fire the navigating event, it fires the new window event. In the new window event, you do not have access to the url or element that was clicked to interrupt the new window and open a default one. Here is how you do it.

First, in your document completed event add the following code.

    private void webBrowserControl_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        string tagUpper = "";

        foreach (HtmlElement tag in (sender as WebBrowser).Document.All)
        {
            tagUpper = tag.TagName.ToUpper();

            if((tagUpper == "AREA") || (tagUpper == "A"))
            { 
                tag.MouseUp += new HtmlElementEventHandler(this.link_MouseUp);
            }
        }
    }

What this does is go through all the html element tags and finds all tags that you define there in the if statement. For my case, I needed AREA tags as well. You can catch any tag you want. We then attach a click event onto that element.

Next we create the event itself with this code.

    void link_MouseUp(object sender, HtmlElementEventArgs e)
    {
        Regex pattern = new Regex("href=\\\"(.+?)\\\"");
        Match match = pattern.Match((sender as HtmlElement).OuterHtml);
        string link = match.Groups[1].Value;

        Process.Start(link);
    }

This code establishes a regex pattern, you'll very likely need to change this pattern for your own needs. I recommend adding a break point here and seeing how your regex needs to look. You can test regex at regex link. Make sure you use parenthesis for grouping. I also recommend using the question mark to not match greedily. We then match on that pattern and grab the url from the match's groups. After that, it's simply a case of starting a process with that url. This will open the default browser, or add a new tab if your default browser is already open.

The last thing we need to do is to cancel the default logic for the links. We do this in the NewWindow event with the following code.

    private void webBrowserControl_NewWindow(object sender, CancelEventArgs e)
    {
        e.Cancel = true;
    }