【发布时间】:2012-08-12 02:38:19
【问题描述】:
假设我有一个包含以下文本的 RichTextbox:
错误
警告
信息
假设我想将最后一个单词的颜色更改为绿色。
我该怎么做?我确实有一些代码可以更改某些文本的颜色(替换功能),但我只想更改最新行,如果您知道我的意思...
我差点忘了,这是我用来改变单词颜色的代码:
static void ReplaceText(RichTextBox box, string phrase, Color color)
{
box.HideSelection = true;
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0; ; )
{
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0) break;
box.SelectionStart = jx;
box.SelectionLength = phrase.Length;
box.SelectionColor = color;
ix = jx + 1;
}
box.SelectionStart = pos;
box.SelectionLength = 0;
}
换句话说,我如何只编辑最新的一行?谢谢!
编辑:顺便说一句!颜色需要保留在那里,比如说控制台输出窗口。
【问题讨论】:
-
所以你不会在新行上拆分,找出数组中有多少个,获取最后一个,然后根据它做你的颜色逻辑吗?
标签: c# winforms richtextbox