I’ve spent a fair bit of time looking for a decent HTML Editor control for a variety of platforms… The platforms on which I needed a decent HTML Editor included Visual Basic 6, .NET Windows Forms and now .NET WPF. The problem is there is no decent native HTML Editor control for any of these, but what’s worse is there aren’t any good third party controls either, so the only option is really to create your own.
Firstly I needed a HTML Editor for VB6. The option that most people seem to suggest is to use the DHTMLEdit OCX which has now been retired but can still be deployed and then call various methods on it to do the editing. You can do something similar with the MSHTML control, but once again it’s quite ugly. I spent some time searching for a better control and found nBit HTML Editor ActiveX Control but after using it for some time found that it produces SUPER UGLY non standards compliant HTML (i.e. 90s style). Given that my content is going onto an XHTML page that is not an option. The control is quite nice in terms of function, but is quite restrictive in what you can do and the ugly HTML is a deal breaker. I didn’t really want to spend heaps of time on implementing all the buttons in DHTMLEdit or the MSHTML by calling execCommand. A frequently referred to post for implementing a Windows Forms HTML Editor was also not of much use as it is very basic and doesn’t fit my needs. Another post on Code Project called WYSIWYG HTML Editor was also not of much use as it didn’t do it how I wanted it.
My initial idea before I began my search was to actually use a WebBrowser control with one of the well established OpenSource HTML Editors like TinyMCE, CKEditor or FCKEditor (now superseded by CKEditor), so I thought I’d see what I can find. I found an article on CodeProject again where someone has implemented TinyMCE HTML Editor in .NET WindowsForms which was pretty close to what i was looking for. I did a bit more searching and also found a blog post where a fella has done the same with FCKEditor, titled TinyMCE HTML Editor running in Windows Forms and another one called FCKEditor HTML Editor Running on Desktop. Between all these posts (and the sample code) I pretty much worked out that this is going to be by far the best way to do this. Some people on various forms and on StackOverflow had taken issue with using the WebBrowser Control in both Windows Forms and WPF as it’s just a wrapper for the COM WebBrowser and they would prefer a native control, but I say who cares, you just need something that works how you want it from a functional perspective and doesn’t take heaps and heaps of code.
I then did a bit more searching to find out how I can avoid any security issues coming up and found a post about WebBrowser in WPF. That showed how to avoid a warning by adding some code to the HTML page.
So putting all of this together I managed to easily implement a WYSIWYG HTML Editor on all of the platforms being VB6, Windows Forms and WPF. What you need to do is actually rather simple, with a few specialty tweaks for each platform:
- Download the WYSIWYG HTML Editor of your choice (i.e. TinyMCE, CKEditor, FCKEditor or some other) and put into your project directory
- Create a HTML file that will host the control. This file has the page that will be loaded in the WebBrowser and provides stuff like initialisation for the control as well as simple getter and setter methods. Make sure to add the stuff at the start of your file to stop the security warning coming up. (i.e. <!– saved from url=(0014)about:internet –>)
- Put a WebBrowser control in your window and in the Load (or Loaded) event use the Navigate function of the WebBrowser to open your file. You can also use the Source or Url Property in WPF and WindowsForms respectively.
- When you run the project your WebBrowser should show now.
- Next thing is to be able to get and set the contents.
That’s pretty much all you need to implement a fully functional HTML Editor. You can of course do stuff like implement a user control or a custom control in WPF and WindowsForms to reuse the web browser easily. You can pass in the configuration and so on from the code and you can even write out the HTML from the code so you don’t need the HTML file. Whichever way you do it is up to you, but this should certainly give you an idea of how to get the basics working, which is the critical part.