WinForms WebBrowser Control: Force all links to open externally in a new (IE) window
-
03-07-2019 - |
Question
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?
Solution
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();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow