开发者

Why isn't the richtextbox displaying this table properly?

开发者 https://www.devze.com 2022-12-14 04:34 出处:网络
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.

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\cel开发者_Python百科lx8500\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?


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;
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消