1、在Form1.Designer.cs添加如下引用代码,否则在窗体代码属性中无法识别DocumentCompleted
2、在窗体代码区域添加如下代码即可,this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
- private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
- {
- try
- {
- //将所有的链接的目标,指向本窗体
- foreach (HtmlElement archor in this.webBrowser1.Document.Links)
- {
- archor.SetAttribute("target", "_self");
- }
- //将所有的FORM的提交目标,指向本窗体
- foreach (HtmlElement form in this.webBrowser1.Document.Forms)
- {
- form.SetAttribute("target", "_self");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("发生错误,原因:" + ex.Message, "报错了", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
文章评论 本文章有个评论