4

Using the IHtmlDocument2.designMode property set to On to switch a WebBrowser control hosted on a Windows Forms form to editing mode suddenly stopped working after installing Microsoft Internet Explorer 9 RC.

Question:

Any chance to fix this?

I already tried to tweak with doctype or with the EmulateIE7 meta tag but without success.

(An example would be this project)


Update 2011-02-21:

As Eric Lawrence suggested, I adjusted the "Zeta" example to set the document text before setting the edit mode.

Unfortunately I did not manage to switch to design mode, either.


Update 2011-02-24:

Parts of the discussion also take place in Eric's blog.


Update 2011-02-26:

What I currently eperience is that the behaviour seems to be different for HTTP URLs and for content that was added via WebBrowser.DocumentText.

First tests seems to prove this assumption.

I'm now going to build a solution around this assumption and post updates and a proof-of-concept here.


Update 2011-02-26 (2):

I've now built a proof-of-concept with a built-in web server which I believe is also working well with IE 9. If anyone would like to download and test whether it is working and give me a short feedback, I can clean up and release the source code for this.


Update 2011-02-26 (3):

No feedback yet, I still updated the HTML Edit Control article and demo over at the Code Project.


Update 2011-03-16:

Since Internet Explorer 9 was released yesterday, we updated our major products to use the idea with the integrated web server as described in the HTML Edit Control article.

After nearly a month of testing, I think it works quite well.

If you do experience any issues in the future with this approach, please post your comments here and I can investigate and fix.

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I have logged this on IE 9 RC feedback along with a link to this page. I have the same issue with my existing code which was based on something at code project. I think this: http://www.codeproject.com/KB/edit/editor_in_windows_forms.aspx – PeteT Feb 27 '11 at 22:52
  • @PeteT Can you post a link here to the IE 9 RC feedback page of your posting? – Uwe Keim Feb 28 '11 at 06:43
  • 1
    Yeah sure I put the feedback in through the IE 9 menu which ends up on their connect site: https://connect.microsoft.com/IE/feedback/details/648138/net-webbrowser-control-wont-enter-design-mode – PeteT Feb 28 '11 at 12:35
  • @PeteT Thanks! Just a note for others clicking on PeteT's link: You first have to join the IE 9 program "connection" thing, otherwise you'll get a "document not found" (or similar) message. – Uwe Keim Feb 28 '11 at 14:37
  • 1
    Oh right sorry I thought it would just link to a live ID sign in if you hadn't joined the connect program. – PeteT Feb 28 '11 at 23:03

9 Answers9

8

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

 ((HTMLBody)_doc.body).contentEditable = "true";
LaughingJohn
  • 101
  • 5
  • 1
    This worked for me. My apps WebBrowser editing now works with ie9. win7-64, ie9-64. – P a u l Apr 20 '11 at 06:27
  • 2
    Very helpful as well... but needed @Michael's code also to get everything solid. – John May 01 '11 at 04:49
  • 1
    @John you Rock! IE keeps making my life harder every time... Little comment, DocumentCompleted uses sender (browser) and Document is HtmlDocument object, I had few issues with casting. Here is property casted solutions in 1 line, if anyone else needs it `(((sender as WebBrowser).Document.DomDocument as IHTMLDocument2).body as HTMLBody).contentEditable = "true";` – Alex Nov 01 '11 at 04:37
2

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.

  • 1
    Lucky you! I did all stuff similar to what you described. While it worked on most machines, I got some where it didn't work :-( Urgently looking for a stable Chrome .NET wrapper... – Uwe Keim Apr 21 '11 at 16:13
1

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.

EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • Thanks a lot, Eric. The basic steps I take (and it worked for years now) is to set the design mode in the `OnDocumentCompleted` event. With the newest version of IE 9 this simply does not switch to design mode anymore (in IE 9 beta, it switched, but do not display the caret anymore). I've tried yet another way of not setting the design mode, but setting `contentEditable` attribute of the `` tag to `true`. ([this is the source for it](http://bit.ly/html-edit-test)). I'll now try to modify my example as you wrote to not set the document text after entering design mode. – Uwe Keim Feb 21 '11 at 08:34
  • One more question: Can you imagine, why the "Zeta" example does not work anymore out-of-the-box with IE9 RC? Did I rely on some undefined behavior of IE6,7 and 8? Or is it a new security restriction? – Uwe Keim Feb 21 '11 at 08:40
1

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.

  • Thanks. I've just built a [proof-of-concept](http://uwe.co/html-editor-test.exe). Does it work with your IE 9 installation? – Uwe Keim Feb 26 '11 at 11:33
  • Thanks! You can try to take a look at my code in the [HTML Edit Control example](http://www.codeproject.com/KB/edit/ZetaHtmlEditControl.aspx) and see whether it is suitable for your need,s too. – Uwe Keim Feb 28 '11 at 06:39
1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I think that method may work. I finally got round to looking at my project and used the webBrowser_GotFocus event to set the designMode. I used this event because my project has multiple web browser controls responding to the same events so it just made it easier for me. – PeteT Mar 21 '11 at 12:37
  • 1
    Cancel that I spoke too soon it does work to put it in designmode but as you said it will wipe out any content you have already added. – PeteT Mar 21 '11 at 14:07
  • @PeteT - Yes, that's why I ended up putting the embedded web server inside the library. I really found no other reliable way than this. – Uwe Keim Mar 21 '11 at 14:32
  • 2
    After playing with my code I have found that setting ((HTMLBody)_doc.body).contentEditable = "true"; in the DocumentCompleted event does work for me after some other changes. I can't say it's ideal but I don't particularly want to add a WebServer either. I think since I have it working for now I will leave it and see if I can implement an open source alternative later. Someone suggested writer found here http://www.lutzroeder.com/dotnet/ I haven't looked at the source yet to see if it relies on anything in the background. – PeteT Mar 21 '11 at 16:05
  • 1
    There is a project [chromiumembedded](http://code.google.com/p/chromiumembedded/) and a .NET wrapper [CefSharp](https://github.com/chillitom/CefSharp) which I tried successfully as a proof-of-concept in the past. Caveats: a) Is currently in alpha version only, b) Webkit seems to have a much more broken HTML editor implementation than MSHTML. – Uwe Keim Mar 21 '11 at 17:49
1

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";
}
Prads
  • 71
  • 2
  • 4
  • Thanks, @Prads - In my tests, it cleared the content when setting the `designMode` to `On`. Maybe I'll give it another try. – Uwe Keim Mar 19 '11 at 17:41
0

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"; 
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
0

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?!?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
0

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!