Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 360
Stefan Koell Avatar asked Mar 01 '09 21:03

Stefan Koell


1 Answers

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();
    }
}
like image 61
Jorge Villuendas Zapatero Avatar answered Nov 19 '22 13:11

Jorge Villuendas Zapatero