Stefan Koell
Stefan Koell

Reputation: 1506

WinForms WebBrowser Control: Force all links to open externally in a new (IE) window

I think the title covers it all. I use the control to display some basic HTML with some markups and there are maybe also links in there. What I want to do, is to force clicking on any link to get this link opened in a new IE window instead of navigating to that page in the control itself.

Any idea?

Upvotes: 6

Views: 2717

Answers (1)

You can handle the Navigating event, set the Cancel property of WebBrowserNavigatingEventArgs to true, and use Process.Start to open the URL in IE.

Something like this:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    // prevents WebBrowser to navigate
    if (e.Url.Host.Length > 0)    // Otherwise the default about:blank when you init the control doesn't work
    {
        e.Cancel = true;

        // Open the URL in an IE window 
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = e.Url.ToString();
        process.Start();
    }
}

Upvotes: 7

Related Questions