webBrowser forced open in this window, prohibit open in a new window

Sometimes you need to load URL with WebBrowser, to perform certain functions. But this time, we do not want the link to open the page open in a new window, because in that case, in fact, is the system default browser to open, and thus out of your WebBrowser, you will not be the control of.
To work around this problem, you can use the following:
suppose WebBrowser's Name is webBrowser
simple way - using the loaded event will target the value of all the links and form changed to "_seft":

void webBrowser_DocumentCompleted Private (Object SENDER, WebBrowserDocumentCompletedEventArgs E) 
{ 
// for all target links, this point form 
the foreach (HtmlElement archor in this.webBrowser.Document.Links) 
{ 
archor.SetAttribute ( "target", "_self") ; 
} 

// all target fORM submission, this point form 
the foreach (HtmlElement form in this.webBrowser.Document.Forms) 
{ 
form.SetAttribute ( "target", "_self"); 
} 
}

  


Copy the code
void webBrowser_DocumentCompleted Private (Object SENDER, WebBrowserDocumentCompletedEventArgs E) 
{ 
// for all target links, this point form 
the foreach (HtmlElement archor in this.webBrowser.Document.Links) 
{ 
archor.SetAttribute ( "target", "_self") ; 
} 

// all target fORM submission, this point form 
the foreach (HtmlElement form in this.webBrowser.Document.Forms) 
{ 
form.SetAttribute ( "target", "_self"); 
} 
}
Copy the code

New window cancel event

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}

The WebBrowser of AllowWebBrowserDrop set to false (Prohibition drag and drop)
the WebBrowser of WebBrowserShortcutsEnabled set to false (prohibiting the use of shortcut keys)
to set the WebBrowser IsWebBrowserContextMenuEnabled false (ban right-click context menu)

Guess you like

Origin www.cnblogs.com/soundcode/p/12456713.html