C# control the color of a character in a string

As far as my current knowledge is concerned, this effect can only be implemented in the RichTextBox control. The general idea of ​​implementation is to select a character or a certain segment of characters and then change the color.


select a character

richTextBox2.SelectionStart = 0;
richTextBox2.SelectionLength = 1;
/*选中内容的起点,选中内容长度,上面两行代码的意思即选择了第一个字符*/

change color

richTextBox2.SelectionColor = Color.Red;
//注意,先选中再变颜色

Of course, for user experience, after changing the color, put the cursor at the end of the string.

ichTextBox2.SelectionStart = text.Length + 1;
richTextBox2.SelectionLength = 1;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979705&siteId=291194637