C# Winforms WebBrowser open links in default browser

12,948

Solution 1

I hope this can help you.

If you want to open a link in a browser, you can add this simple code:

Process.Start("http://google.com");

Remember, there is a lot of information about it. Here in stack Overflow you can take a look in this post: How to open in default browser in C#


If you want to open your link in another browser, you can use this code:

System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");

Don't forget to visit this post called: How do I open alternative webbrowser (Mozilla or Firefox) and show the specific url?


And Finally, I could recommend you this stack overtflow post called: .NET C#: WebBrowser control Navigate() does not load targeted URL

I hope this information can help you a little bit.

Solution 2

This is an old post but I believe I may understand what the original poster wanted to do. They wanted a page to load in the webbrowser control if the user selected it from the dropdown list but any links in the loaded page should open in the user's web browser. If this is indeed the case, the original poster need a flag on the form to determine the behavior.

The original poster simply needed a flag such as linksOpenInSystemBrowser shown below.

using System;
using System.Windows.Forms;

namespace Browser_Test
{
    public partial class myForm : Form
    {
        private bool linksOpenInSystemBrowser = false;

        public myForm()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            linksOpenInSystemBrowser = false;
            webBrowser1.Navigate(comboBox1.SelectedItem.ToString());
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if(!linksOpenInSystemBrowser)
            {
                linksOpenInSystemBrowser = true;
                return;
            }

            System.Diagnostics.Process.Start(e.Url.ToString());

            e.Cancel = true;
        }
    }
}
Share:
12,948
sd_dracula
Author by

sd_dracula

Updated on June 04, 2022

Comments

  • sd_dracula
    sd_dracula almost 2 years

    I know this has been discussed several times around here but the default behaviour for opening links clicked in a WebBrowser control does not work for my application.

    So while this works as in it opens a link clicked in IE:

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

    I am using a dropdown list to update the html file that the webBrowser is displaying like so:

    private void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
       webBrowser1.Url = myURI;  
    }
    

    Now the problem I'm having is that with the _Navigating method above, the webBrowser does not load any subsequent changes to the URL (thanks to the e.cancel I guess) so it only displays the first html file it loads.

    If I remove the _Navigating method it updates fine but then the links open up in the same webBrowser control which is what I do not want.

    How can I get it to work both ways?