RichTextBox cannot display Unicode Mathematical Alphanumeric Characters

I cannot get WinForms RichTextBox display some Unicode characters, especially Mathematical alphanumeric characters (but the problem is most likely not limited to these).

Surprisingly, the same characters can be displayed in a simple or multi-line TextBox using the same font (default). Even if I change the font, for example, β€œArial” or β€œLucida”, I get the same results.

TextBox vs. RichTextBox

Screenshot from Windows 10, but I get the same results in Windows 7. This example shows ascii small ad and then the sans serif mathematical small alpha triangle.

I am using Visual Studio 2017 and .NET 4.6.1.

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 this does not seem to be a problem with character preservation. Characters are correctly stored in RichTextBox . If you copy text and paste it somewhere (for example, in a TextBox ), all characters will be displayed correctly.

On the other hand, if you insert characters into a RichTextBox , you will get the same wrong display.

So this only seems to be a display problem.

+7
source share
1 answer

This is a bug / project decision in RichTextBox that was fixed in .NET 4.7.

RichTextBox is actually a wrapper around RichEdit . In .NET 4.7, the control uses the RICHEDIT50W , whereas in previous versions it used the RichEdit20W .

To solve the problem, you can use one of the following options:

  • Upgrade to .NET 4.7

OR

  • You can use the latest version of RichTextBox , i.e. RICHEDIT50W , for this you should inherit from the standard RichTextBox and override CreateParams , also load the Msftedit.dll library and set ClassName to RICHEDIT50W .

To see the implementation, check out this post .

+10
source

Source: https://habr.com/ru/post/1273574/


All Articles