richtextbox SelectionColor 和 SelectionBackColor 属性

Ani*_*oel 4 c# richtextbox winforms

我想选择特定的文本行并用蓝色突出显示它,并且我希望该文本的前色为白色。我试过

 this.Select(start, length);
 this.SelectionBackColor = Color.Blue;
 this.SelectionColor = Color.White;
Run Code Online (Sandbox Code Playgroud)

但它不起作用。怎么了?我想模拟当我们通过鼠标选择一些文本时得到的效果,它的背景色变为浅蓝色,内部文本变为白色。我只要这样做就可以得到它

 this.Select(start, length);
Run Code Online (Sandbox Code Playgroud)

但一旦它失去焦点,选择就会消失,我希望它永久存在。

Kat*_*ate 5

有更简单的方法来为 RichTextBox 中的文本着色:

richtTextBox.SelectionColor = Color.Red;
richTextBox.SelectedText = "Red text";
richtTextBox.SelectionColor = Color.Green;
richTextBox.SelectedText = "Green text";
Run Code Online (Sandbox Code Playgroud)

你得到: 在此输入图像描述