12

I cannot get WinForms RichTextBox display some Unicode characters, particularly Mathematical alphanumeric symbols (but the problem is most probably not limited to those).

Surprisingly the same characters can display in a plain or multiline TextBox using the same (default) font. Even if I change the font to for example "Arial" or "Lucida", I get the same results.

TextBox vs. RichTextBox

The screenshot is from Windows 10, but I'm getting the same results on Windows 7. The example shows ascii small a-d followed by mathematical italic sans-serif small alpha-delta.

I'm using Visual Studio 2017 and .NET 4.6.1.

A trivial test code:

private void InitializeComponent()
{
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    // ...
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(25, 38);
    this.textBox1.Multiline = true;
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(182, 108);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "abcd 𝛼𝛽𝛾𝛿";
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(213, 38);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(179, 108);
    this.richTextBox1.TabIndex = 1;
    this.richTextBox1.Text = "abcd 𝛼𝛽𝛾𝛿";
    // ...
}

Note that it does not seem to be a problem with storing the characters. The characters are correctly stored in the RichTextBox. If you copy the text out and paste it somewhere (like to the TextBox), all characters display correctly.

On the other hand, if you paste the characters to the RichTextBox, you get the same incorrect display.

So it looks like a display problem only.

0

1 Answer 1

14

It's a bug/decision by design in RichTextBox which has been fixed in .NET 4.7.

RichTextBox is in fact a wrapper around RichEdit. In .NET 4.7, the control is using RICHEDIT50W while in previous versions it's using RichEdit20W.

To solve the problem you can use either of these options:

  • Upgrade to .NET 4.7

OR

  • You can use the latest version of RichTextBox that is RICHEDIT50W, to do so you should inherit from standard RichTextBox and override CreateParams and load library Msftedit.dll and set the ClassName to RICHEDIT50W.

To see an implementation, take a look at this post.

3
  • 1
    I changed to 4.7 and now working fine. Thank you so much @Reza Aghaei. Commented Nov 6, 2020 at 5:24
  • 2
    its still happening in 4.8 Commented Nov 11, 2020 at 1:09
  • @JadaVonRuth I didn't try recently, but I checked the source code of RichTextBox, and I expect it work properly. Make sure you haven't truned on the Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl in AppContextSwitchOverrides. Commented Nov 11, 2020 at 19:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.