NOTICE: As of 10/28/2017, a new version is available.

Insert inline images either by URL or through the file system. File system images are embedded in the resulting HTML.

Full programmatic access to HTML, Body HTML, Body Text

Set the background color of the body.

Insert images.

Resize images.

Add and edit hyperlinks from text.

Add and edit hyperlinks from images.

Support for Windows XP SP2 to Windows 8.

Support for all versions of IE.

Introduction

A while ago, I was working on a chat application where one of the chat clients was web based and written with ASP.NET 2.0. The other chat client was a Windows Forms based .NET application written in C#. I needed a rich text editor for my WinForms client that could output HTML, so that the ASP.NET client could display the formatting from the WinForms client. This was ruled out while using the RTF text box.

The solution I worked out was to use a WebBrowser control in edit mode within my WinForms client. Formatting buttons on a toolbar above the web browser are synchronized to the current selection in the WebBrowser control.

This article explains solutions to most of the issues involved in building an editor control from a WebBrowser control. I won't go into everything, as the source code isn't that difficult to browse. But, I do cover some of the tricks necessary to get this to work.

Using the Control

The control is an executable.

You can run it directly with it's main form, or you can embed it as a control in your own app.

To embed it into your own app, simply reference the EXE as if it were a DLL from your app in Visual Studio.

It should show up in the Toolbox Window in Visual Studio.

Check the source code for examples of how to access it from your code.

It's a .NET 2.0 control, so it should work in older projects.

Setting Design Mode

Applying design mode and establishing an editing template for the document occurs automatically when using the component. But, for reference, here is a brief explanation of how it works.

Applying design mode requires using a COM interface: adding a reference to "Microsoft HTML Object Library", which resolves to MSHTML, and adding a 'using' of 'MSHTML'.

It is necessary to add a body to the control before you can apply changes to the DOM document. To do this, you can simply apply some text to the DocumentText property of the WebBrowser control.

webBrowser1.DocumentText = "<html><body></body></html>"

Next, get a reference to the new DomDocument COM interface, and set the design mode to "On".

Synchronizing Formatting Buttons with Selected Text

This is a bit more tricky than applying formatting commands to the browser control. I literally query the state of the browser editor selection every 200 ms, and set the toolbar formatting buttons based on this.

You'll notice that there is a Tick event that is fired at the end of the timer's tick event. External components can subscribe to this event to update the state of their GUI. For example, they may update the Enabled state of cut/copy/paste/undo/redo buttons based on the state of the Editor control.

I do this by using the COM document interface retrieved from the WebBrowser control with:

The link button and font controls are managed in a similar way, but I'll leave that for you to check out in the code.

Focusing

Focusing the control isn't necessarily simple either. The WebBrowser control itself will not accept focus. Neither will the WebBrowser control's document. But, the body will focus, assuming there is a body element on the control.

Of course, you never need to call this method directly. Calling the Focus method on the control will place the focus on the editor control containing the WebBrowser control. The editor control will transfer focus to the WebBrowser's document body automatically, when it receives the GotFocus event.

Retrieving Text or HTML

The BodyHtml and BodyText methods retrieve the body HTML and text, respectively.

Linking to the Component Assembly

In Visual Studio 2005, you can link to an assembly (add a reference), even if the assembly is an executable. The editor is written as a component embedded in a form, so you can add this component to the control palette and drag and drop into your application. The control's name is Editor, under namespace Design.

Conclusion

The .NET 2.0 WebBrowser control can effectively be used as a text editor. This is useful when you need an editor control that can be used as an HTML editor. The implementation is not completely straightforward in some areas. This article attempts to show some of the tricks necessary to get it to work.

Firstly sorry for posting the reply so late... I have found the solution but forgot completely to post it.

I am not sure if this is the best way to do it but it works for me

Change the webBrowsers GotFocus as follow:

private void webBrowser1_GotFocus(object sender, EventArgs e)
{
SuperFocus();
//Set the Text on the comboBox to the required font.
fontComboBox.Text = "Tahoma";
// Execute the TextChanged Funtion to apply the changes on the webBrowser
fontComboBox_TextChanged(null, null);
// Set the seleted tex of the comboBox back to blank.
fontComboBox.Text = "";

The same idea applies for the default font size:

//Set the Text on the comboBox to the required font.
fontSizeComboBox.Text = "10";
// Execute the TextChanged Funtion to apply the changes on the webBrowser
fontSizeComboBox_TextChanged(null, null);
// Set the seleted tex of the comboBox back to blank.
fontSizeComboBox.Text = "";

I am using windows form webbrowser control and dynamically changing the document text.
When ever i change the document text am getting the following Dialog Message but i want to disable it.
This document has been modified. Do you want to save changes?

I know the people having this issue have likely resolved it by now, but for those of us searching here is what I found worked. I had a slightly different experience so neither of the solutions offered on this thread worked. I was using a secondary Windows form as a pop-up/input to capture/modify the actual HTML source code and return it to the browser form. I did not need to mess with header rather only the body so if you need to do that you may need to find a different method. My specific problem was when the Source Edit form was launched more than once I could not view any of my changes, and I was also getting the demand to save the file. Solution:

Instead of using the WebBrowser.DocumentText property use WebBrowser.Document.Body.InnerHTML

I want to insert a custom tag to the control like <mytag id="1">ABC. I want to insert this in between document's text. Can anyone guide me how could I do this and I want to make sure that in design mode it should show only 'ABC' text not the tag <mytag> .

The IHTMLTxtRange.PasteHTML() should do the trick. I used [most of] this control in a Windows Forms project I'm working on. I had a requirement where I needed to "inject" some text into the beginning, middle or end of the HTML created by this control. Here's the method I came up with to do it: