Search code examples
vb.netwebbrowser-controlmshtml

Keep WebBrowser's content when entering in Design Mode


I have this WebBrowser extension class with a boolean property named .DomDesignMode which sets whether DomDocument is in design mode or not.

However, when I set this property to true, the browser clears all its previous content, loaded through setting .DocumentText property.

Is there a way of making it enter design mode and still keep its previous HTML content? Thanks.

Imports System.Windows.Forms
Imports mshtml

Public Class WebBrowserEx
    Inherits WebBrowser

    Private ReadOnly Property DomDocument As mshtml.IHTMLDocument2
        Get
            If Document IsNot Nothing Then
                Return Document.DomDocument
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Property DomDesignMode As Boolean
        Get
            If DomDocument IsNot Nothing Then
                Return String.Equals(DomDocument.designMode, "on", StringComparison.InvariantCultureIgnoreCase)
            Else
                Return False
            End If
        End Get
        Set(value As Boolean)
            If DomDocument IsNot Nothing Then
                DomDocument.designMode = If(value, "on", "off")
            End If
        End Set
    End Property

End Class

Solution

  • Now, let's forget DomDocument, this is what one needs:

    WebBrowser1.Document.Body.SetAttribute("contentEditable", "true")
    

    Many thanks to this answer available here in StackOverflow!