Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Some Alt keys changes my RichTextBox font

I am creating some Hindi typing application in Windows Forms.

I have used KrutiDev010 Font, there are some alt key control codes for some characters but if I enter some other alt key control codes, like Alt+0261, Alt+025896 then it prints a charters ?, respectively and changes my textbox font to "Arial".

How to block only those alt key control codes which change my RichTextBox fonts?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This is the default behavior.
RichTextBox automatically finds a fallback font to represent those characters that the currently specified 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 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 behaviour in your specific context.
If you insert an Unicode symbol that is not handled by the current Font, 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...