Tuesday, 03 May 2011

tinyMCE running in Windows Forms

This article has moved to my new blog at the URL:

Brutal Developer: tinyMCE running in Windows Forms

13 comments:

Anonymous said...

I find this very interesting, but have a question. I know this post is over a year old but, In testing this code, I have an error saying that adding url, picture or whatnot is not possible as a popup blocker is enabled. I have disabled all pop-up blockers in both my IE browser and Firefox, but still get this error. Any thoughts as to why?

Thanks

~Karnith

Werner van Deventer said...

Hi Karnith,

I have updated the code to use the latest version of tinyMCE (v3.2.4.1 at the time of writing) This allows the pop-up to appear.

The control seems to have some serious issues with IE8 especially when it's running from the local machine. I'm sure there are some workarounds which I will investigate soon. In the meantime, the updated code download works.

Regards,
Werner

Karnith said...

Hi Werner,

this works great!. What did you add/change in the coding? I looked at the htm file, but it looks like nothing was changed there. I tried myself (before your post by adding inlinepopups to the init and got passed the popup error, but it wouldn't display the contents). I can see several uses for this and plan to integrate it into my project. I assume that one could use mysql db tables instead of writing to a file using OleDB or mysql connector/net? I'm fairly new to C# and so far am amazed at what it can do. Although the drawbacks would be not being able to use c# programs in a linux environment, would this be correct in saying?

~Karnith

Werner van Deventer said...

Hi Karnith,

I went back to the tinyMCE website and checked if they had sorted out any issues regarding IE8. They did so I tried the latest version and also noticed there were additional plugin options on their example page. I added the extras and it seems to work relatively well from there.

You can get and set the Html of the control in anyway you want, you just need to ensure that you encode the HTML so that it is rendered correctly.

If you want to use MySQL then you will have to get your hands on a third party provider since the framework only comes with SQL Server and Oracle native providers built in. The code for all providers will be very similar if not the same. Get a MySQL provider here. If you plan on using LINQ over MySQL, check out this page here.

Writing applications in .NET has virtually no drawbacks when it comes to Linux. As long as all your code is managed (does not contain native Win32 calls) then it will run on Linux using Mono. Since Mac uses a Linux based operating system, your .NET apps will run on Mac as well with Mono installed.

Hope this helps you out in starting your journey using .NET and C#.

Regards,
Werner

Matt Williamson said...

Exactly what I was looking for! Well done, thank you.

Unknown said...

I want to load content at startup based on a command line argument (fileName).

It does not seem to work after CreateEditor() and also not on MainForm_Shown().

Where should I put it.

Werner van Deventer said...

The web browser hasn't finished loading the document by the time the windows form has loaded so those options won't work. I tried subscribing to the DocumentCompleted event on the web browser control but that was still too early because the script to set the content hadn't loaded either.

The solution is hacky but works quite well. Just change the CreateEditor method to run a timer that loads the HTML that you send in.

public void CreateEditor(string html)
{
  // Check if the main script file exist being used by the HTML page
  if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"tinymce\jscripts\tiny_mce\tiny_mce.js")))
  {
    webBrowserControl.Url = new Uri(@"file:///" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tinymce.htm").Replace('\\', '/'));
    if (!string.IsNullOrEmpty(html))
    {
      Timer loadTimer = new Timer();
      loadTimer.Interval = 1000;
      loadTimer.Tick += delegate
                  {
                   try
                   {
                    HtmlContent = html;
                    loadTimer.Stop();
                   }
                   catch { }
                  };
      loadTimer.Start();
    }
  }
  else
  {
    MessageBox.Show("Could not find the tinyMCE script directory. Please ensure the directory is in the same location as tinymce.htm", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

Now you can just read the file contents in the Load event of your form and pass the HTML into CreateEditor.

Anonymous said...

This is really useful - thank you!

I have it working a treat in a Windows Form.

The only problem I have is that I would really like to launch this form from a vsts custom control. If I try this it works up to a point, but when I click a button that opens up a new window (e.g. view html source, insert table etc.) I get an error "Call was rejected by callee".

I don't suppose you have any idea how I might be able to work around that do you?

(I'm using vs.net 2008, Windows 2003, IE7)

Anonymous said...

The code worked with your version of tinymce. However it does not seem to be working with the latest release. If I delete the latest version folder in my project and replace it with version in your solution, it works.

To include in a windows form application, do I have to include tinymce folder and tinymce.htm in the bin-debug folder?

Anonymous said...

Hello,

Thanks for this code.

I am having issues setting the control to accept some text.
Hence in HtmlContent, an exception is thrown as webBrowserControl.Document is null

Also, when calling CreateEditor(), the Url property is null after executing the statement in your code.

Werner van Deventer said...

Question 1:
I don't not what exactly you need, I just copied the contents of the library to a location and referenced it. There have been problems in the past where newer versions don't load up nicely. They don't seem to maintain backward compatibility very nicely. The concept behind this trick is quite simple, you should be able to get any basic web form functionality loaded in a win forms dialog, certain things will be finnicky and you just need to figure those out as you go along.

Question 2:
It is likely that you are trying to make this set before the control is able to load. Try changing the code the way I specified in this comment. It allows the tinyMCE editor to load up first before accepting commands to modify it.

Anonymous said...

hi karnith,

this is very interisting, but i have one question. i want to load content at startup and make it read only html. can you give me the way to do that?

Werner van Deventer said...

To load content on start up just use the code in the comment above.

To load read-only content you should just load the static page into a web browser control. This editor route is for editing files not really displaying them.