Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

c# - RichTextBox cannot display Unicode Mathematical alphanumeric symbols

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...