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