阻止WebBrowser Control自动添加<a> Tag when contentEditable is set

时间:2016-11-08 10:54:56

标签: c# winforms webbrowser-control

I use WebBrowser control and have text inside like:

some text https://www.example.com some text.

This is my code the way I added the text and enabled the web browser to edit contents:

public partial class Form1 : Form
{
    private HTMLBody _body;

    public Form1()
    {
        InitializeComponent();
        webBrowser1.Navigate("about:blank");
    }

    private void webBrowser1_DocumentCompleted(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
    {
        _body = ((HTMLBody)((HTMLDocument)webBrowser1.Document.DomDocument).body);
        _body.contentEditable = true.ToString();
        _body.innerHTML = "some text https://www.example.com some text";
    }
}

If I run it and changed part of link (for type something else instead of 'example.com' and lost focus) then it automatically adds tag <a> around my link. You can see it in innerHTML property. But it's wrong for me.

Is there a way to avoid to do this behavior? Thanks a lot!

1 个答案:

答案 0 :(得分:0)

您可以在文档中关闭自动提示网址。为此,在为body设置新内容后,在DocumentCompleted evnet中添加以下代码行:

webBrowser1.Document.ExecCommand("AutoUrlDetect", false, false);

此外,您可以在不添加mshtml.dll引用的情况下编辑内容。为此,您只需使用:

this.webBrowser1.DocumentText = @"<HTML><BODY contentEditable=""true""></BODY></HTML>";