简体   繁体   English

更改RichTextBox字体,保持文本颜色

[英]Change RichTextBox Font keeping text color

I have a lot of formatted code snippets saved in an XML file. 我在XML文件中保存了许多格式化的代码段。 On request I load them and set as Rtf in a RichTextBox: 根据要求,我加载它们并在RichTextBox中设置为Rtf:

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. 这些代码段是从Visual Studio复制粘贴的,因此文本的颜色是不同的。 Different Visual Studios are using different Fonts for the text. 不同的Visual Studio对文本使用不同的字体。

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. 我尝试设置RichTextBox的Font属性,但这也会重置颜色。

//This changes colors as well
rtb_cs.Font = new Font(FontFamily.GenericSansSerif, 10);

Take a look at LarsTech's solution here: 在这里查看LarsTech的解决方案:

Changing font for richtextbox without losing formatting 更改Richtextbox的字体而不会丢失格式

It works for other settings too. 它也适用于其他设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM