5,661,954 members and growing! (15,986 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate License: The Code Project Open License (CPOL)

tinyMCE HTML editor in .NET WindowsForms

By Ondra Spilka

tinyMCE WYSIWYG HTML editor in .NET WindowsForms
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows, IE, WinForms, Dev

Posted: 7 Jul 2008
Updated: 7 Jul 2008
Views: 7,769
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.85 Rating: 3.67 out of 5
2 votes, 33.3%
1
1 vote, 16.7%
2
0 votes, 0.0%
3
1 vote, 16.7%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Had you in past need to incorporate some HTML validating editor into your .NET desktop client? I did, so I started to look around for some components and found one, which is free and widely used, tinyMCE form Moxito.

One complication is that this one uses browser to render edited content and is completely written in JScript, so one have to “hack” some browser control.

I’ll describe how to incorporate an IE ActiveX into .NET form, join needed soap of tinyMCE into work file and load this work file into IE ActiveX.

Background

As I said tinyMCE is JScript wrapper over browser textarea control.

One possible solution is to use some browser control, load it with content and let the browser to do the work with Jscript. Let’s assume, we’re on Windows platform, so IE is incorporated in system.

So what we need is to store somewhere tinyMCE package, generate work HTML file, which contains edited text and links to tinyMCE scripts.

I decided to use users application folder, special subfolder will be created and tinyMCE package as like as workfile will be stored.

Last thing to solve is to look over tinyMCE and check what it needs to work. And it’s not too much, in minimal version only few lines of code.
Let’s take simple example based on tinyMCE package examples:

<html>
<head>
<title>tinyMCE
minimal example</title>
 
<!—SETTINGS
section -->
<!--
TinyMCE -->
<script type="text/javascript" src="../jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
       tinyMCE.init({
              mode : "textareas,save",
              theme : "simple"
       });
</script>
<!-- /TinyMCE -->
 
</head>
<body>
 
<!—tiny MCE section -->
<form method="post" action="http://tinymce.moxiecode.com/dump.php?example=true">
       <textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%">
        This is Edited Text
       </textarea>
</form>
 
</body>
</html>       

Looking around this snippet, one can see we need to construct settings section and tiny MCE section. Settings are needed for tinyMCE to tell what plugins to load, what encoding and format of the text we need etc., and second section is textarea element encapsulated in form element, and this is what tinyMCE hooks and use as editing area.

Last point about “design” - it has to be reusable around few applications, so I encapsulated everything in one form class FrmHTMLBrowser, which is enabled to view or edit text. Why not in control? Well, tinyMCE is not pure control, we have to use some “hacks” with temp files, so it is a bit safer and more robust to use modal dialog box. Programmers, who will use this form, will be forced to use it more carefully and declaratively, not allowed to just paste control in designer whereever they think is fine.

One little thing to think about - browser needs to be JScript in path or subpath where workfile resides. That's why we have to place everything in one place. Basically, no matters where, if in ApplicationData\LocalSettings ot \Temp or wherever else, but in one place.

Think also about running your application on terminal services - that's why I chose user ApplicationData. No problem with interfering forms.

Note: Problem with interfering forms will arise when same applications will run concurrently in same user profile and will be of same editor type (same Type parameter in EditText). But this is not my case, if yours, try to use more specific storage or workfile indexing (find first "free" name). tinyMCE package can be shared, that's not a problem.

"line-height: normal;" class="MsoNormal">

Using the code

"line-height: normal;" class="MsoNormal">I used some helper methods to get it everything work in FrmHTMLBrowser.

private string
getWorkPath(string subPath)

this one gets path to temporary storage where work file and tinyMCE package are stored. Actually returns subfolder M_WORKPATH in Environment.SpecialFolder.LocalApplicationData

public void PrepareTinyMCE()

checks if tinyMCE already exists, if not, unzips package using ICSharp.ZipLib

public Stream GetResourceFile(string File)

gets stream from embedded resources

public string EditText(string Text, string Type, string CSSFile)

Public method for editing text, covers and calls showText.
Type
allows you to store work file with different name, so you can call simultaneously few edit forms in one running user profile (theoretically, it’s not this case while form is shown modal).

private void showText(bool edit) 

Main method. Here, HTML work file with links to tinyMCE Jscript is constructed and stored into temporary file.
There’s a little bit custom code in this method, CSS replacement. Method looks in settings for [CSS] string and replaces it with passed path to CSS. Why this is usefull? Well, when you’ll use this form eg. as editor for product descriptions over multiple regional databases of multiple companies, You’ll need to pass different CSSs got from database for example.

Aftre constructing work page and preparing tinyMCE package browser is navigated to work file by calling

axBrowser.Navigate(sDoc);

User stores text by pressing Save button in tinyMCE, that’s why in setting section it’s declared tinyMCE have to use save plugin. One more hack, when save is used, one need to cancel postback action of the form otherwise will get tinyMCE error message. Looking into implementation leads to simple solution in showText:

sb.AppendLine("<form method=\"\" action=\"\" onsubmit=\"return false;\">"); 

That’s it, smart guys in Moxito think about it, cancelling onsubmit will cancel error message too.

Last thing, how to get edited text back?

m_text = ((mshtml.IHTMLDocument3)axBrowser.Document).getElementById("elm1").innerText;

Retyping root DOM document of the IE control to IHTMLDocument3 allows you to access document elements represented by DOM model, so it’easy to get content of textarea elm1, where tinyMCE resides.

Use FrmHTMLBrowser class to create editor enabled window just as in example.

tinyMCE and settings are stored in resources as embedded resource, you can of course modify this solution and use disk storage or whatever you imagine.

Call FrmHTMLBrowser as you need, you'll get content of the editor back.

  FrmHTMLBrowser brw = new FrmHTMLBrowser();
  ctlRtf.Text = brw.EditText(ctlRtf.Text, "tempfilename", "yourCSS.css");  

Here in "tempfilename" you can specify name of work file, yourCSS.css will specify path to CSS file (of course again, it'll be copied into work folder and make as relative path to packcage) , which will be used by tinyMCE to fillup Style combobox.

Links

tinyMCE is © of Moxito, http://tinymce.moxiecode.com/
sharpZIPLib is http://sharpdevelop.net/OpenSource/SharpZipLib

Points of Interest

It's not my point of interest, HTML editors. Rather HW, Neural Networks, signal processing. But as in real life, I needed some HTML editor, and this one is very good and free under GPL.

History

Implemented, tested 20080707

License

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

About the Author

Ondra Spilka



Occupation: Software Developer (Senior)
Location: Czech Republic Czech Republic

Other popular Edit Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralExactly like my solutionmemberpeitor5:39 24 Jul '08  
GeneralLoad errors;memberGlen Harvy4:26 12 Jul '08  
GeneralRe: Load errors;memberOndra Spilka5:45 12 Jul '08  
GeneralRe: Load errors; [modified]memberGlen Harvy13:46 12 Jul '08  
GeneralProblemmemberkedysoft21:37 11 Jul '08  
GeneralRe: ProblemmemberOndra Spilka5:48 12 Jul '08  
GeneralRe: Problemmemberkedysoft23:15 13 Jul '08  
General[Message Removed]memberMojtaba Vali19:12 8 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2008
Editor:
Copyright 2008 by Ondra Spilka
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project