How to open a link in webBrowser control in external browser?

前端 未结 6 1465
轮回少年
轮回少年 2020-12-15 19:39

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

相关标签:
6条回答
  • 2020-12-15 19:47

    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());
    }
    
    0 讨论(0)
  • 2020-12-15 19:47

    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.

    0 讨论(0)
  • 2020-12-15 19:54

    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!!

    0 讨论(0)
  • 2020-12-15 19:59

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-15 20:07

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

    0 讨论(0)
  • 2020-12-15 20:14

    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;
        }
    
    0 讨论(0)
提交回复
热议问题