C # prevent webbrowser from opening the link page in a new window

Source: Internet
Author: User

In daily development, you sometimes need to use webbrowser to load URLs to implement some functions. At this time, we do not want to open the link in the page, open in the new window, because in this case, it is actually opened in the default browser of the system, thus breaking away from your webbrowser, you cannot control it.

To solve this problem, you can use the following method:

Assume that the name of webbrowser is webbrowser1.

Private void webbrowserincludocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs e) {// point the target of all links to the form foreach (htmlelement archor in this. webbrowser1.document. links) {archor. setattribute ("target", "_ Self");} // point all the form submission targets to the form foreach (htmlelement form in this. webbrowser1.document. forms) {form. setattribute ("target", "_ Self ");}}
Private void webbrowser1_newwindow (Object sender, canceleventargs e) {e. Cancel = true ;}
 
Remember to set allowwebbrowserdrop of webbrowser to false.
Set webbrowsershortcutsenabled of webbrowser to false
Set iswebbrowsercontextmenuenabled of webbrowser to false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.