如何在FCTB(快速颜色文本框)中为文本中的一个单词上色

本文关键字:文本 一个 单词 FCTB 颜色 | 更新日期: 2023-09-27 18:07:00

我可以在c#中创建一个快速颜色文本框,并很容易地添加文本:

FastColoredTextBox tb = new FastColoredTextBox();
this.Controls.Add(tb);
tb.Location = new Point(10, 10);
tb.Visible = true;
tb.Text = "This is some text to display in the FCTB.";

我不明白如何将文本中的一个单词更改为不同的颜色。

我不想通过语法来识别单词,我的应用程序更像是一个文字处理器,用户希望用颜色来强调一个单词。

例如,我如何将上面代码段中的单词"some"更改为绿色而不是黑色?

谢谢

如何在FCTB(快速颜色文本框)中为文本中的一个单词上色

我终于找到了我一直在寻找的成员函数。

方法如下。

  FastColoredTextBox tb = new FastColoredTextBox();
  this.Controls.Add(tb);
  tb.Location = new Point(0, 0);
  tb.Visible = true;
  tb.Text = "This is some text to display in the FCTB.";
  // define a new Style... specifically a TextStyle
  Style greenstyle = new TextStyle(Brushes.Green, Brushes.White, FontStyle.Bold);
  // select the range of characters to modify
  Range rng = new Range(tb, 8, 0, 12, 0);
  // change the display to green
  rng.SetStyle(greenstyle);