Internet Explorer 9 RC stops my WinForms WebBrowser control to work in editing mode

淺唱寂寞╮ 提交于 2019-11-28 08:49:23

I had a similar problem and got around it by adding the following line to the DocumentCompleted event:

 ((HTMLBody)_doc.body).contentEditable = "true";

We just need an empty editable control. I did however step through debugger and add value to the control's InnerHtml and it displayed it fine, and I could edit it.

Small update, we were able to get the control editable using this line also:

browserControl.browser.Document.Body.SetAttribute("contentEditable", "true");

This allows us to avoid referencing mshtml, (don't have to include Microsoft.mshtml.dll)

This lets us avoid increasing our installation size by 8 megs.

What's your exact code?

If I set the following code:

    private void cbDesign_CheckedChanged(object sender, EventArgs e){
        var instance =
    Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(
    wbView.ActiveXInstance,
    null,
    @"Document",
    new object[0],
    null,
    null, null );

         var objArray1 = new object[] { cbDesign.Checked ? @"On" : @"Off" };

    Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex(
    instance,
    null,
    @"designMode",
    objArray1,
    null,
    null,
    false,
    true );

The IE9 Web Browser instance enters designMode without any problems. If you change the "Zeta" example to not set the document text after entering design mode, it also works fine.

Just want to add that I am also unable to enter designmode (using a WebBrowser control in my case). This was not an issue in the beta. Definitely new with the RC.

Another Code Project user suggested to use the following code:

First, add event DocumentCompleted:

private void SetupEvents()
{
    webBrowser1.Navigated += webBrowser1_Navigated;
    webBrowser1.GotFocus += webBrowser1_GotFocus;
    webBrowser1.DocumentCompleted += this.theBrowser_DocumentCompleted;
}

Then write the function:

private void theBrowser_DocumentCompleted(
    object sender, 
    WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.Write(webBrowser1.DocumentText);
    doc.designMode = "On";
}

Although I did not test this, I want to document it here for completeness.

It's fixed if the property is set after the document is loaded

private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

    IHTMLDocument2 Doc = Document.DomDocument as IHTMLDocument2;
    Doc.designMode = @"On";
}

Yesterday, Internet Explorer 9 RTM finally was released.

I did some more tiny adjustments to my control, but basically the idea with the intergrated, small web server seems to work rather well.

So the solution is in this Code Project article:

Zeta HTML Edit Control
A small wrapper class around the Windows Forms 2.0 WebBrowser control

This was the only solution that worked for me.

I hope it is OK to answer my own question and mark my answer as "answered", too?!?

I was also able to get this to work using the following inside the DocumentCompleted event:

IHTMLDocument2 Doc = browserControl.browser.Document.DomDocument as IHTMLDocument2;
if (Doc != null) Doc.designMode = @"On";

Thanks everyone!

user2015772

I use HTML Editor Control, I solved this problem adding the DocumentComplete event

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    (((sender as WebBrowser).Document.DomDocument as IHTMLDocument2).body as HTMLBody).contentEditable = "true"; 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!