Change RichTextBox Font keeping text color - c#

I have a lot of formatted code snippets saved in an XML file.
On request I load them and set as Rtf in a RichTextBox:
string cscode = #"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fprq1\fcharset0 Courier New;}{\f1\fnil\fcharset0..." etc.
rtb_cs.Rtf = cscode;
The code snippets are copy-pasted from the Visual Studio, so the text is colored differently.
Different Visual Studios are using different Fonts for the text.
Is there a way to change the Font but keep the colors?
I've tried to set the Font property of the RichTextBox, but this also resets the colors.
//This changes colors as well
rtb_cs.Font = new Font(FontFamily.GenericSansSerif, 10);

Take a look at LarsTech's solution here:
Changing font for richtextbox without losing formatting
It works for other settings too.

Related

Set the font in winforms designer to a Font object

I'm working on a project in Windows Forms and I want to use custom fonts for buttons etc.
I have a static class that contains the styles of fonts that I want. For example:
ParagraphFont = new Font(Properties.Resources.Font, 14.25F);
TitleFont = new Font(Properties.Resources.Font, 48, FontStyle.Bold);
I want to be able to apply the font to the form/user control through the designer, but I can't figure out how to do this.
I've tried setting the fonts of all the controls to these variables after the InitialiseComponent() method, but this seems long-winded and tedious to do to the multiple forms and controls that my project has
Is there a quicker way of doing this?
Edit: My question is different to the suggested duplicate. I don't need to set the default font of the project, I want to set the font of each control individually to a style variable. For example:
label.Font = ParagraphFont;
In the designer. This way, I can change the font used in ParagraphFont at any time

WPF character spacing irregular in RichTextBox

I have a problem with character spacing.
Basically I have something like this which comes from a txt file:
****************
*System Details*
****************
Looks nice and uniform, however, when I open have this go into a RichTextBox this happens:
Irregular character spacing example:
I've tried all different properties to try and stretch it, render it etc. but nothing works.
The data is coming in from code-behind OpenDialogBox that stores all the lines of the file in a string[]. A foreach loop then sends the lines into the RTB. (It needs to be a loop as each line gets checked)
Any help it greatly appreciated!
Many thanks
This is most likely a font choice problem. By default WPF uses Segoe UI on Windows 7 and above which is a non-monospaced font. This means that each character will not necessarily take up the same amount of space as each other character leading to issues if you are trying to align characters between lines. The easiest way to get alignment to work is by changing the font to a monospaced font by setting the FontFamily property on the RichTextBox.

Change Font.Size in a RichTextBox .SelectionFont with multiple FontFamily

How can I change the Font.Size in a RichTextBox .SelectionFont (which has two or three different FontFamily), without affecting the FontFamilyand the FontStyle
This one works fine, if I have only one Font.Family
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, 12);
But, if I have more, it throws nullexception.
Perhaps this answer from the question: "Reset RTF in RichTextBox?" may help you. I quote:
More you ask?
I use most of this via a small utility class that wraps this for all the styles and font changes. This way you can change font-size and not change font name, etc.
I've had the same problem. Check the answer by LarsTech here:
Changing font for richtextbox without losing formatting
You'll find properties such as SelectionFontName, SelectionFontSize, ...

How to load a font file and use its font style in C#?

I can load the font file using this:
PrivateFontCollection _fonts = new PrivateFontCollection();
_fonts.AddFontFile ( filepath );
Font customFont = new Font(_fonts.Families[0], 6.0F);
But, the problem that I am facing is that I cannot load the font style (Bold/Italics etc) from the font file.
I need the font file from the user, because I am going to save the font file and then use OpenGL to render it. But, before the actual rendering, I need to show a preview using WPF.
All the fonts can be assumed to be system fonts. But, I need to find out the font style from TTF file to show it on WPF Canvas. What I can actually do is that I can ask the user to load a font file as well as specify the style from the drop down, but that defeats the purpose, because if the user specifies a wrong style, then it will show differently on the emulator and during rendering.
So, what should I do?
I'm not sure that I completely understand your problem, but if you want to display some text in a particular font, then you can do it like this. First, add the font file into a folder in your project... let's say it's called Resources. Next, set its Build Action to Content. Then you can use that font in XAML like this:
<TextBlock FontFamily="/Resources/#Some Font Name" Text="Some Font Name" />

How to add "styled" documentation tooltip in C#?

How do I add "styled" (bold, italic etc.) tips in C#? Or is there any way to do this?
PS: That code on image doesn't work.
The "styling" in your image is not part of the C# specification. It's just your editor (Sublime?) that does some basic parsing, and sets the text color of items it recognizes.
You can see for yourself if you open your so-called "styled" source code in a plain text editor such as TextEdit or Notepad. Even if you can see colors again, they are assigned by that software in turn.

Categories

Resources