I think I've found a bug in Windows or .NET and am looking for a workaround.

To reproduce the problem, first enable the Windows feature "Beta: Use Unicode UTF-8 for worldwide language support". This is found here. You may need to reset the machine.

Now simply create two RichTextBox components in Winforms/C#, and then add the event:

    private void richTextBox1_TextChanged(object sender, EventArgs e)
        string s = richTextBox1.Rtf;
        richTextBox2.Rtf = s;

Finally, run the program and simply type something into the first RichTextBox, and it'll crash with the message "File format is not valid" when it tries to write to the richTextBox2.Rtf. It won't crash if the Windows feature "Beta: Use Unicode UTF-8 for worldwide language support" is disabled.

I'm thinking of two potential workarounds here:

1: Somehow disable within the C# app the entire "Beta: Use Unicode UTF-8 for worldwide language support" feature and pretend it was never enabled in the first place.

2: Somehow edit the RTF string to comply with whatever unknown requirements the new RTF should have before adjusting the RTF of the other RichTextBox. This seems counter-intuitive considering the first RichTextBox should have exactly the same RTF anyway, but anyway...

************* Exception Text **************
System.ArgumentException: File format is not valid.
at System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
at System.Windows.Forms.RichTextBox.StreamIn(String str, Int32 flags)
at System.Windows.Forms.RichTextBox.set_Rtf(String value)
at unicodeTesting.Form1.richTextBox1_TextChanged(Object sender, EventArgs e) in D:\Code\c#\_tests\unicodeTesting\Form1.cs:line 30
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at System.Windows.Forms.TextBoxBase.OnTextChanged(EventArgs e)
at System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WmReflectCommand(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

0 Answers