Some Alt keys changes my RichTextBox font

This is the default behavior.
The RichTextBox Control automatically finds a fallback font to represent characters that the current Font can’t handle.
If not otherwise instructed, it changes the Font selection with the fallback Font.

Read this rant about the same default behavior of the RichTextBox ancestor (the RichEdit / MsftEdit Control, from which the WinForms control derives): Don’t change the font, dammit!

There are means to change this behavior. At least in the way the RichTextBox Control reports the current Font and Font selection. The C++ code in the previous blog post is one.

A StackOverflow Question/Answer on the subject:
How to force a Win32 RichEdit fallback to SimSun with East Asian text.

But you don’t need to fallback to C++ for this.
See the RichTextBox.LanguageOption and the RichTextBoxLanguageOptions flags.

You can insert this line of code right after the InitializeComponent() proc in your Form Constructor:

richTextBox1.LanguageOption = RichTextBoxLanguageOptions.DualFont;

or

richTextBox1.LanguageOption = RichTextBoxLanguageOptions.DualFont | 
                              RichTextBoxLanguageOptions.UIFonts;

There’s a slight difference. See the reference Docs and test this behavior in your specific context.
If you insert an Unicode symbol that the current Font cannot handled, a fallback is always possible, but the RichTextBox UI will not change the Font, not even the SelectionFont.
And Clipboard.SetText(), Clipboard.GetText() will work as usual.

Leave a Comment