Why isn't the richtextbox displaying this table properly?
Asked Answered
C

2

10

Apparently, the RichTextBox provided by Microsoft doesn't fully support the RTF specs. For some reason, it won't permit multi-lined rows, and destroys formatting instead.

Forexample, here is the RTF code to generate a table:

\par
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl Length of Time until Replayment\cell\cell Flate Fee Percentage\cell\cell Broker and Application Fees\cell\cell Total lien on case\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 0-6 \cell Months \cell 40% \cell\cell 310 \cell\cell\{#TOTALLIEN0-6#\}\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 7-12 \cell Months \cell 60% \cell\cell 310 \cell\cell\{#TOTALLIEN7-12#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 13-18 \cell Months \cell 100% \cell\cell 310 \cell\cell\{#TOTALLIEN13-18#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 19-24 \cell Months \cell 150% \cell\cell 310 \cell\cell\{#TOTALLIEN19-24#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 25-36 \cell Months \cell 200% \cell\cell 310 \cell\cell\{#TOTALLIEN25-36#\} \cell
\row\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 37+ \cell Months \cell 300% \cell\cell 310 \cell\cell\{#TOTALLIEN37#\} \cell

This works fine if both word and wordpad. The top row where the text is too long breaks into multipule lines, however, in the Richtext box it does something wacky.

Wordpad looks like this: wordpad RTF table http://img231.imageshack.us/img231/2720/wordpadrtf.jpg

And the Richtext box looks like this: richtextbox table http://img262.imageshack.us/img262/9756/richtextboxrtf.jpg

How can I make the richtextbox work properly?

Corenda answered 18/12, 2009 at 15:25 Comment(0)
C
18

I found the solution. Evidently, there are more than one RichEdit library on every system, and the default to an older version (4.0 I think). 5.0 has fixed most of the problems with the RTF interpretation. To get a RichtextBox that uses it, you must inert RichTextBox, and overload the CreateParams property.

Here is how I did it:

public partial class FullRichtextBox : RichTextBox {
    public FullRichtextBox() :base() {
        InitializeComponent();
    }
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);

    protected override CreateParams CreateParams {
        get {
            CreateParams param = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero) {
                param.ClassName = "RICHEDIT50W";
            }
            return param;
        }
    }
}
Corenda answered 18/12, 2009 at 16:46 Comment(3)
Here's another one, it handles the scrollbars better: social.msdn.microsoft.com/Forums/en-US/winforms/thread/…Unaesthetic
Top answer, thank you! 'Select is actually broken this time.Everetteeverglade
Thanx for the solution, I wish I can vote 15 times to your solution :)Pettus
S
0

I recently came across a similar problem and while the accepted answer worked, I also found an interesting solution on this stack overflow q&a: https://stackoverflow.com/a/48391710

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
   </startup>
   <runtime>
      <AppContextSwitchOverrides
         value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=false" />
   </runtime>
</configuration>

This meant that I could continue to use .NET 4.5.2 with the latest RichTextBox without creating a custom class, etc. Although, I should probably just update to .NET 4.7+ at some point.

Sexist answered 25/2, 2020 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.