Evgeny Gonokhov Evgeny Gonokhov - 1 month ago 8
C# Question

c# WebBrowser control automatically add tag <a> after text was changed

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!

Answer

You can turn off auto-dettecting url in document. To do so, in DocumentCompleted evnet after you set the new content for body, add this line of code:

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

Also you can make the content editable without adding reference to mshtml.dll. To do so you can simply use:

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