Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows.Forms.RichTextBox Loses table background colours

When loading a rtf file into a Windows Forms RichTextBox it loses the background colour of table cells. If we use a WPF RichTextBox and load the same file everything is formatted as it should.

Am I missing something when I load the file into the Windows Forms RichTextBox?

Windows Forms RichTextBox code snippet :

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
        fDialog.Filter = "Rich Text Files (*.rtf)|*.rtf";
        fDialog.Multiselect = false;
        fDialog.RestoreDirectory = true;
        if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (fDialog.FileName != "")
            {
                richTextBox1.LoadFile(fDialog.FileName, RichTextBoxStreamType.RichText );
            }
        }
    }

In the above code snippet I have also tried using

richTextBox1.Rtf = File.ReadAllText(fDialog.FileName);

and

richTextBox1.LoadFile(fDialog.FileName);

WPF RichTextBox code snippet

    private void load_file_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
        fDialog.Filter = "Rich Text Files (*.rtf)|*.rtf";
        fDialog.Multiselect = false;
        fDialog.RestoreDirectory = true;
        if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (fDialog.FileName != "")
            {
                FileStream fStream;
                fStream = new FileStream(fDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                richtextbox1.SelectAll();
                richtextbox1.Selection.Load(fStream, DataFormats.Rtf);
                fStream.Close();
            }
        }

    }

Here is the screen shot from both versions : enter image description here

Thanks in advance for any help.

Steve.

like image 656
SteveP Avatar asked Dec 18 '15 14:12

SteveP


1 Answers

There were many versions of RichTextBox, Winforms locked-in an early release, version 2.0. Goes back to .NET 1.x and .NET 2.0, versions that could still run on ancient Windows versions like 98. Support for tables in v2.0 is lacking.

That's eminently fixable, it doesn't take much code to upgrade the version. Version 5.0 is available on XP and up. All you have to do is load the native DLL, msftedit.dll instead of riched20.dll, so that the "RichEdit50W" window class becomes available. And override CreateParams to use that class.

Add a new class to your project and paste the code shown below. Compile. You can drop the new control from the top of the toolbox, replacing the old one.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class RichTextBox5 : RichTextBox {
    protected override CreateParams CreateParams {
        get {
            if (moduleHandle == IntPtr.Zero) {
                moduleHandle = LoadLibrary("msftedit.dll");
                if ((long)moduleHandle < 0x20) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");
            }
            var cp = base.CreateParams;
            cp.ClassName = "RichEdit50W";
            return cp;
        }
    }
    private static IntPtr moduleHandle;

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

A sample table I created with Word rendered perfectly:

enter image description here


UPDATE: this code is now built-in to Winforms, target at least version 4.7 to take advantage of it.

like image 124
Hans Passant Avatar answered Nov 12 '22 09:11

Hans Passant