Click here to Skip to main content
12,243,074 members (50,891 online)
Click here to Skip to main content
Add your own
alternative version


394 bookmarked

A Windows Forms based text editor with HTML output

, 2 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A Windows Forms based text editor with HTML output, implemented with a browser control in edit mode.

NOTICE: As of 6/30/2013, a new version is available. 

Includes the following features:

  • 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. 

Sample Image - editor.png


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".

IHTMLDocument2 doc =
webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";

Finally, I replace the context menu for the web browser control so the default IE browser context menu doesn't show up.

webBrowser1.Document.ContextMenuShowing += 
new HtmlElementEventHandler(Document_ContextMenuShowing);

The browser is now in design mode, with a custom method to display the context menu.

Applying Formatting

You can apply formatting and editor functions to a browser control in design mode with the ExecCommand method on browser.Document.

Here are several examples:

public void Cut()
    webBrowser1.Document.ExecCommand("Cut", false, null);
public void Paste()
    webBrowser1.Document.ExecCommand("Paste", false, null);
public void Copy()
    webBrowser1.Document.ExecCommand("Copy", false, null);

Some commands will toggle the formatting state of the current selection.

public void Bold()
    webBrowser1.Document.ExecCommand("Bold", false, null);

public void Italic()
    webBrowser1.Document.ExecCommand("Italic", false, null);

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.

private void timer_Tick(object sender, EventArgs e)
    boldButton.Checked = IsBold();
    italicButton.Checked = IsItalic();
    underlineButton.Checked = IsUnderline();
    orderedListButton.Checked = IsOrderedList();
    unorderedListButton.Checked = IsUnorderedList();
    linkButton.Enabled = SelectionType == SelectionType.Text;

    if (Tick != null) Tick();

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:

IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;

Then I use queryCommandState to determine the state of the current selection:

public bool IsBold()
    return doc.queryCommandState("Bold");

public bool IsItalic()
    return doc.queryCommandState("Italic");

public bool IsUnderline()
    return doc.queryCommandState("Underline");

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 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.

private void SuperFocus()
    if (webBrowser1.Document != null && 
        webBrowser1.Document.Body != null)

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.


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.


  • 10/04/2006 - various bug fixes and design updates / designer support.
  • 10/19/2006 - more bug fixes and more designer support.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

kevin delafield
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionsize of font 7 and unordered list bullets dots size Pin
nighttrain_23-Mar-16 10:23
membernighttrain_23-Mar-16 10:23 
Questionissue Pin
Member 1211573929-Jan-16 1:08
memberMember 1211573929-Jan-16 1:08 
QuestionProblem using VS2015 Pin
susad4-Jan-16 23:07
membersusad4-Jan-16 23:07 
QuestionControl not supporting rich text editing Pin
Digambar Malla18-Nov-15 0:11
memberDigambar Malla18-Nov-15 0:11 
Questionto change font size to pixel format? Pin
Member 1214844517-Nov-15 20:40
memberMember 1214844517-Nov-15 20:40 
QuestionPesky save Dialog fix Pin
Member 1160865517-Nov-15 3:32
memberMember 1160865517-Nov-15 3:32 
QuestionThe type or namespace name 'LiveSwitch' could not be found Pin
only4ram12-Nov-15 20:21
memberonly4ram12-Nov-15 20:21 
AnswerRe: The type or namespace name 'LiveSwitch' could not be found Pin
Member 188040317-Nov-15 2:15
memberMember 188040317-Nov-15 2:15 
QuestionMessage Removed Pin
Digambar Malla9-Oct-15 2:19
memberDigambar Malla9-Oct-15 2:19 
QuestionFinding where the cursor is in the document Pin
elmbrook29-Sep-15 17:30
memberelmbrook29-Sep-15 17:30 
AnswerRe: Finding where the cursor is in the document Pin
Member 1211573929-Jan-16 1:13
memberMember 1211573929-Jan-16 1:13 
SuggestionCOM Interface and the dynamic keyword Pin
Paulo Morábito Neto18-Sep-15 7:48
professionalPaulo Morábito Neto18-Sep-15 7:48 
QuestionHello Kevin, excellent, thanks. I had to tweak it as it would not work in my app Pin
Leif Simon Goodwin23-Aug-15 6:32
memberLeif Simon Goodwin23-Aug-15 6:32 
QuestionWhy don't local images appear Pin
Matt Esterak16-Jun-15 5:15
memberMatt Esterak16-Jun-15 5:15 
QuestionHow can i disable and enable that editor tooltip Pin
sheelakr12-May-15 21:28
membersheelakr12-May-15 21:28 
AnswerRe: How can i disable and enable that editor tooltip Pin
DrDeadCrash21-Jul-15 9:38
memberDrDeadCrash21-Jul-15 9:38 
QuestionImage is not display in the editor Pin
Member 115717051-May-15 3:23
memberMember 115717051-May-15 3:23 
QuestionEmail not read HTML Pin
Member 1039338130-Apr-15 21:07
memberMember 1039338130-Apr-15 21:07 
QuestionError when closing form Pin
robin11215-Mar-15 9:52
memberrobin11215-Mar-15 9:52 
QuestionCode generation for property 'BodyBackgroundColor' failed Pin
Member 1146258718-Feb-15 13:14
memberMember 1146258718-Feb-15 13:14 
AnswerRe: Code generation for property 'BodyBackgroundColor' failed Pin
Digambar Malla18-Nov-15 0:06
memberDigambar Malla18-Nov-15 0:06 
GeneralRe: Code generation for property 'BodyBackgroundColor' failed Pin
L30P1N018-Feb-16 16:53
memberL30P1N018-Feb-16 16:53 
QuestionIs it possible to store the text in data base?> Pin
st_24916-Jan-15 2:54
memberst_24916-Jan-15 2:54 
Questiondialog Pin
Fox NO.123-Dec-14 17:47
memberFox NO.123-Dec-14 17:47 
Questionunable to open saved file Pin
vikaskallidantheyil21-Dec-14 19:12
membervikaskallidantheyil21-Dec-14 19:12 
QuestionIt doesn't show online images in a html page Pin
puramini21-Nov-14 9:33
memberpuramini21-Nov-14 9:33 
QuestionRemove MessageBox about:Blank T Pin
Member 1045614117-Sep-14 21:20
memberMember 1045614117-Sep-14 21:20 
AnswerRe: Remove MessageBox about:Blank T Pin
Member 1066388020-Feb-16 2:09
memberMember 1066388020-Feb-16 2:09 
QuestionGreat article, one problem though... Pin
Sander Rossel21-Aug-14 6:20
professionalSander Rossel21-Aug-14 6:20 
QuestionRe: Great article, one problem though... Pin
BadJerry26-Aug-14 4:33
memberBadJerry26-Aug-14 4:33 
AnswerRe: Great article, one problem though... Pin
Sander Rossel26-Aug-14 4:40
professionalSander Rossel26-Aug-14 4:40 
GeneralRe: Great article, one problem though... Pin
BadJerry27-Aug-14 0:31
memberBadJerry27-Aug-14 0:31 
GeneralRe: Great article, one problem though... Pin
Sander Rossel27-Aug-14 3:09
professionalSander Rossel27-Aug-14 3:09 
GeneralRe: Great article, one problem though... Pin
susad9-Dec-15 4:29
membersusad9-Dec-15 4:29 
GeneralRe: Great article, one problem though... Pin
Sander Rossel9-Dec-15 9:24
professionalSander Rossel9-Dec-15 9:24 
Questionerror Pin
MercuryNiny3-Mar-14 7:48
memberMercuryNiny3-Mar-14 7:48 
Bugworks fine with .net framework 2.0 error when changed to .net framework 4.0 Pin
ytcargo4-Feb-14 2:33
memberytcargo4-Feb-14 2:33 
GeneralRe: works fine with .net framework 2.0 error when changed to .net framework 4.0 Pin
MercuryNiny3-Mar-14 22:32
memberMercuryNiny3-Mar-14 22:32 
GeneralRe: works fine with .net framework 2.0 error when changed to .net framework 4.0 Pin
Fernandobhz21-Oct-14 7:07
memberFernandobhz21-Oct-14 7:07 
QuestionCannot add to toolbox in VS2012 Pin
Daniel Borson31-Jan-14 12:22
memberDaniel Borson31-Jan-14 12:22 
QuestionRe: Cannot add to toolbox in VS2012 Pin
emti8526-Jun-14 2:20
memberemti8526-Jun-14 2:20 
QuestionHow to...? Someone? Pin
Zero The Warrior1-Dec-13 9:01
professionalZero The Warrior1-Dec-13 9:01 
QuestionLicense Pin
nokama7-Nov-13 10:43
membernokama7-Nov-13 10:43 
Bugediting contents Pin
stelios198429-Sep-13 0:20
memberstelios198429-Sep-13 0:20 
GeneralMy vote of 5 Pin
susanna.floora26-Sep-13 22:59
membersusanna.floora26-Sep-13 22:59 
QuestionLine Spacing Pin
susanna.floora26-Sep-13 22:58
membersusanna.floora26-Sep-13 22:58 
GeneralMy vote of 5 Pin
Viper20109-Sep-13 14:55
memberViper20109-Sep-13 14:55 
QuestionControl in a DOT.NET 2012 with integrated Pin
Member 935623920-Aug-13 4:02
memberMember 935623920-Aug-13 4:02 
AnswerRe: Control in a DOT.NET 2012 with integrated Pin
Member 935623920-Aug-13 4:20
memberMember 935623920-Aug-13 4:20 
QuestionLine Spacing Pin
strader758-Aug-13 8:35
memberstrader758-Aug-13 8:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 2 Jul 2013
Article Copyright 2006 by kevin delafield
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid