5

I have a problem with .Net's RichTextBox control. It seems that it doesn't support table cell formatting, which is funny because most of the time I create tables I want the cell contents to be right-aligned (numbers, currency).

If I try to open a WordPad document in RichTextBox, it ignores (and actually removes) the commands for cell alignment. I tried several workarounds but didn't succeed.

  1. Can anyone think of an idea to fix this? (without using fixed-width fonts and spaces) This would be the best solution since other code is working fine already, so if only thing needed is a dirty hack, it would be great.

  2. Or is there an open source alternative for .Net Rich Text Editor you can recommend? I need a user control I can embed in my Windows form and access the contents programmatically (create content, or append something). I have searched the web for some time but found only web (Ajax/Javascript) controls.

  3. There are also HTML WYSIWYG editors which I could use, but they are all basically a IE browser embedded and edited using MSHTML, and it feels a bit strange to have that in a Winforms app (maybe I am wrong). And in that case we will need some extra time to implement a content generator for HTML - although it's much easier to read and generate than RTF IMHO.

  4. What do you guys find best for this purpose?

vgru
  • 49,838
  • 16
  • 120
  • 201

2 Answers2

9

If you are still going down the .net winforms path then inherit from RichTextBox and add the following code, it will transform the RichTextBox into something "useable":

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

protected override CreateParams CreateParams
{
    get
    {
       CreateParams cparams = base.CreateParams; 
       if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
       {
          cparams.ClassName = "RICHEDIT50W";
       }
       return cparams;
     }
}

Sourced from here.

Have a nice day:)

EricLaw
  • 56,563
  • 7
  • 151
  • 196
Hoffmania
  • 926
  • 2
  • 7
  • 15
  • Thanks a lot, I will try it. At the end we stayed with RichText the way it is, the customer didn't mind its issues that much. – vgru Oct 02 '09 at 12:29
  • Yes, this looks promising. This version of RichTextEdit seems to support a wider range of Rtf specs, although it renders some stuff a bit differently from the old one, so we will have to do some modification in our Rtf generator. Thanks! – vgru Oct 05 '09 at 06:49
  • 2
    Some lite benchmark data: Styling a 723 line XML document loaded into a control based on this example dropped the time taken from 1700ms to 1200ms, so an appreciable performance boost, but still leaves a bit to be desired overall from MS' rich edit controls in general. – Jace Oct 09 '13 at 16:22
2

3.There are also HTML WYSIWYG editors which I could use, but they are all basically a IE browser embedded and edited using MSHTML, and it feels a bit strange to have that in a Winforms app (maybe I am wrong).

I've written a HTML WYSIWYG editor: the ModelText HTML Control for .NET. It's pure managed code, with no dependency on a browser; it exports .NET APIs, which let you access its contents programmatically.

The next version to be released (in a few days from now) will support cell alignment (by supporting the CSS "text-align" property).

ChrisW
  • 54,973
  • 13
  • 116
  • 224