Full Version: Creating a wysiwyg HTML editor in C#
Dream.In.Code > Programming Tutorials > C# Tutorials
aj32
Creating a wysiwyg HTML editor in C#

This tutorial will demonstrate how to create a wysiwyg (visual) HTML editor using IHTMLdocument2 class in mshtml.


The first thing we need to do is create a windows forms application in C#, using an IDE such as Visual Studio or SharpDevelop.

On the form, place a toolbar and a webbrowser control, such as shown below

IPB Image

Next, we need to set the properties of the webbrowser to make it work correctly in edit mode:

webbrowser properties:

AllowNavigation = false
Allowwebbrowserdrop = false
IsWebBrowserContextMenu = false
ScriptErrorsSuppressed = true
(Name) = HTMLEditor


All other settings can remain their default value


Now we need to add a reference to mshtml, do this by clicking the 'Project' menu bar item, and, from the drop down menu, click 'Add Reference'.
When the dialog appears, click the COM tab. Under that tab, scroll down untill you see 'Microsoft HTML Object Library', double click the object to add it.


Now that we have the reference to the HTML object library, which resolves to 'mshtml', just add a using mshtml; to our Form1.cs file.
Also, under public partial class Form1 : Form, add this global variable:
csharp

private IHTMLDocument2 doc;



Once this is done, go back to design mode, double click the top part of the form to create a Form1_Load()( event. In Form1_Load(), add the following code:

csharp

HTMLEditor.DocumentText = "<html><body></body></html>"; //This will get our HTML editor ready, inserting common HTML blocks into the document
//Make the web 'browser' an editable HTML field

doc =
HTMLEditor.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
//What we just did was make our web browser editable!


We now have a fully enabled wysiwyg HTML editor!


Controlling the HTML Editor

Now that we have our editor set up, we need a way to edit the contents to the user's commands, add something to the toolbar we inserted a while ago, such as a bold button, font size selector, etc. For this tutorial, I am going to use a bold button and a font size selector.

IPB Image

Double-Click the Bold button, creating an onclick() event. You should be automatically switched to code view. In the event that was just created (eg. private void toolStripButton1_Click() ), add this to make the selected font bold:

csharp

HTMLEditor.Document.ExecCommand("Bold", false, null);


...and that's it, this will now make the selected text bold!

The first argument ("Bold") is, obviously, the command. The Second argument (false in this case) determines whether or not the pre-made dialog, that comes with mshtml, is shown. The third argument (null in this case) is the Object Value.


Do the same thing for the rest of the controls you have added to your toolbar, create your own WYSIWYG HTML editor!

Here is a list of some of the most common commands:
csharp

HTMLEditor.Document.ExecCommand("Bold", false, null);
HTMLEditor.Document.ExecCommand("Underline", false, null);
HTMLEditor.Document.ExecCommand("Italics", false, null);
HTMLEditor.Document.ExecCommand("StrikeThrough", false, null);
HTMLEditor.Document.ExecCommand("FontName", false, "Times New Roman");
HTMLEditor.Document.ExecCommand("FontName", false, "Arial");
HTMLEditor.Document.ExecCommand("FontName", false, "etc.");
HTMLEditor.Document.ExecCommand("FontSize", false, "1");
HTMLEditor.Document.ExecCommand("FontSize", false, "2");
HTMLEditor.Document.ExecCommand("FontSize", false, "3");
HTMLEditor.Document.ExecCommand("InsertUnorderedList", false, null);
HTMLEditor.Document.ExecCommand("InsertOrderedList", false, null);
HTMLEditor.Document.ExecCommand("Cut", false, null);
HTMLEditor.Document.ExecCommand("Copy", false, null);
HTMLEditor.Document.ExecCommand("Paste", false, null);
HTMLEditor.Document.ExecCommand("CreateLink", true, null);


//HERE IS THE WAY TO INSERT YOUR OWN TEXT INTO THE HTML EDITOR:


String TEXT = "YOUR TEXT GOES HERE!";

IHTMLTxtRange range =
doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(TEXT);
range.collapse(false);
range.select();



There are a few more, but those are the most common ones!



Getting the HTML from the control



If you want your HTML editor to save the html to a .html file, just call the DocumentText from the webbrowser, like this:
csharp


String SAVECONTENTS = HTMLEditor.DocumentText;

//Continue, to save the contents to a file




This is the end of this tutorial! I hope this tutorial has been useful to you, and thanks for reading!
If you have any questions on this topic, feel free to ask!
-AJ32 smile.gif
aj32
I can't edit the tutorial, but I'll post some more of the HTML editor commands here:
(Most are self-explanatory)

csharp

//Images:
string IMGSRC = "http://www.example.com/example.jpg";
HTMLEditor.Document.ExecCommand("InsertImage", false, IMGSRC);

//LINK:
string URL = "http://www.example.com";
string Text = "Example.com";

IHTMLTxtRange range =
doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML("<A href=\"" + URL + "\">" + Text + "</A>");
range.collapse(false);
range.select();

//HR:
HTMLEditor.Document.ExecCommand("InsertHorizontalRule", false, null);

//ALIGN:
HTMLEditor.Document.ExecCommand("JustifyLeft", false, null);
HTMLEditor.Document.ExecCommand("JustifyRight", false, null);
HTMLEditor.Document.ExecCommand("JustifyCenter", false, null);
HTMLEditor.Document.ExecCommand("JustifyFull", false, null);




(if one of the mods could add that into the tutorial, that would be great wink2.gif )
A7med Baraka
when i add this

Now that we have the reference to the HTML object library, which resolves to 'mshtml', just add a using mshtml; to our Form1.cs file.
Also, under public partial class Form1 : Form, add this global variable:
csharpview plainprint?
private IHTMLDocument2 doc;


there is 2 errors appers
Error 1 The type or namespace name 'mshtml' does not exist in the namespace 'System' (are you missing an assembly reference?) Form1InterActive Book.cs 8 14

Error 2 The type or namespace name 'IHTMLDocument2' could not be found (are you missing a using directive or an assembly reference?) Form1InterActive Book.cs 14 17


but i am adding the reference as u said ??

any advice ??
aj32
QUOTE(A7med Baraka @ 14 Jul, 2008 - 07:01 AM) *

when i add this

there is 2 errors appers
Error 1 The type or namespace name 'mshtml' does not exist in the namespace 'System' (are you missing an assembly reference?) Form1InterActive Book.cs 8 14

Error 2 The type or namespace name 'IHTMLDocument2' could not be found (are you missing a using directive or an assembly reference?) Form1InterActive Book.cs 14 17


but i am adding the reference as u said ??

any advice ??


It looks like your are trying to add System.mshtml it is only mshtml (e.g. using mshtml;. Once you get that in place the IHTMLDocument2 error will go away (IHTMDocument2 is a part of mshtml).

Hope that helps. smile.gif
A7med Baraka
thnx i make it it should write MSHTML in capital
but i want to add audio , flash files ?? how can i make this
also i want to ask how i make the user inter the url of the image or the audio file or its location in the PC
Kirth
QUOTE(A7med Baraka @ 19 Jul, 2008 - 04:57 AM) *

also i want to ask how i make the user inter the url of the image or the audio file or its location in the PC


You could put those in a folder called "images" and get them uploaded with the user's files?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.