© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
1 reply
HimmDawg

Replacing font table in RichTextBox (WinForms)

I'm working on a .NET Framework 4.8 app that uses WinForms and in there,
RichTextBox
RichTextBox
is used.

The app has a list of supported fonts, so we made an algorithm that sorts out unknown fonts when you type or paste text with fonts that are not on the list. If an unknown font ist detected every character having an unknown font will be set to a default font from that list.

Now, somebody copied a text from a pdf and pasted it into a richtext box.
The fonts in the table are
Times New Roman
Times New Roman
,
TimesNewRoman
TimesNewRoman
,
TimesNewRoman,Bold
TimesNewRoman,Bold
. So the algorithm (correctly) detects
TimesNewRoman
TimesNewRoman
,
TimesNewRoman,Bold
TimesNewRoman,Bold
as unknown and tries to replace those. But in reality, it didn't do anything because the text didn't contain characters with one of those two fonts, thus leaving the two unknown fonts in the fonttable.

I collected the fonts actually contained in the text with
IEnumerable<string> actualFonts = new List<string>();

for (int i = 0; i < riText.TextLength; i++)
{
  riText.Select(i, 1);
  actualFonts.Add(riText.SelectionFont.FontFamily.Name);
}
IEnumerable<string> actualFonts = new List<string>();

for (int i = 0; i < riText.TextLength; i++)
{
  riText.Select(i, 1);
  actualFonts.Add(riText.SelectionFont.FontFamily.Name);
}

...and in there, I got 3x
Microsoft Sans Serif
Microsoft Sans Serif
(different font sizes) and 2x
Times New Roman
Times New Roman
(different font sizes). I'm a bit lost here. Manually replacing the fonttable doesn't work. What can I do?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

richtextbox winforms
C#CC# / help
3y ago
Font ligatures in richtextbox?
C#CC# / help
4y ago
WinForms change RichTextBox selection font size but keep style
C#CC# / help
3y ago
Add custom font in c# winforms
C#CC# / help
2y ago