简体   繁体   中英

Open new page in standard browser when link is clicked

I can open the link in my standard browser with this code:

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());
}

But the problem is that IE only should be opened when a link on the webbrowser is clicked. When using this code IE also opens when I change the documenttext.

My suggestion would be to take a different approach. At the point in time immediately after the initial page has loaded in the WebBrowser control (Navigated event), you can use the webBrowser1.Document property to retrieve an HtmlDocument instance.

From this you should be able to find your link by using, for example,

http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx

Then you can add an event handler to detect when this link is clicked, and in this handler, run your code to start the IE process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM