1

let's say I have a RichTextbox that contains the following text:

Error
Warning
Info

And let's say I want to change the last word's color to green.
How would I do that? I do have some code to change the color of some text (a replace function), but all I want is to ONLY change the latest line if you know what I mean...

I almost forgot, here is the code that I use to change the word's colors:

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;
    }

So in other words, how do I ONLY edit the latest line? Thanks!

EDIT: BTW! The colors need to stay there, it's let's say a console output window.

4

2 回答 2

4

这对你有用:

public void HighlightLastLine(RichTextBox TextControl, Color HighlightColor)
{
    TextControl.Text = TextControl.Text.Trim();
    TextControl.SelectionStart = 0;
    TextControl.SelectionLength = 0;
    TextControl.SelectionColor = Color.Black;
    string LastLineText = TextControl.Lines[richTextBox1.Lines.Count() - 1];
    int LastLineStartIndex = richTextBox1.Text.LastIndexOf(LastLineText);
    TextControl.SelectionStart = LastLineStartIndex;
    TextControl.SelectionLength = TextControl.Text.Length - 1;
    TextControl.SelectionColor = HighlightColor;
}

用法:

HighlightLastLine(richTextBox1, Color.Indigo);

基本上,我们在这里所做的是先进行一些清理,然后使用 Lines 字符串数组选择控件的最后一行。然后我们得到该字符串的最后一个索引(如果我们有重复),我们只是告诉控件从行的开头开始着色并一直到结尾。最后,我们应用在参数中传递的颜色。

编辑:

添加一个允许自定义文本突出显示的重载,并且如果在 ClearColors 参数中传递了一个错误标志,也不会清除文本。

  public void HighlightLastLine(RichTextBox TextControl, string TextToHighlight, Color HighlightColor, bool ClearColors = true)
    {
        TextControl.Text = TextControl.Text.Trim();
        if (ClearColors)
        {
            TextControl.SelectionStart = 0;
            TextControl.SelectionLength = 0;
            TextControl.SelectionColor = Color.Black;
        }
        int LastLineStartIndex = richTextBox1.Text.LastIndexOf(TextToHighlight);
        if (LastLineStartIndex >= 0)
        {
            TextControl.SelectionStart = LastLineStartIndex;
            TextControl.SelectionLength = TextControl.Text.Length - 1;
            TextControl.SelectionColor = HighlightColor;
            TextControl.SelectionStart = 0;
            TextControl.SelectionLength = 0;
        }
    }

用法:

HighlightLastLine(richTextBox1, "Michael Jackson", Color.Indigo, false);

这将尝试找到迈克尔杰克逊的最后一个索引并将其着色。请注意,在最后一个参数中提供了一个 false ,这将允许保留现有颜色。

于 2012-08-12T02:53:55.473 回答
1

感谢DelegateX!这对我有用...

    public void HighlightLastLine(RichTextBox rtb1, Color fgColour)
    {
        if (rtb1.Text != "")
        {
            // Cleanup leading/trailing whitespace
            rtb1.Text = rtb1.Text.Trim();
            rtb1.SelectionStart = 0;
            rtb1.SelectionLength = 0;
            rtb1.SelectionColor = Color.Black;
            // Get index of first char on last line
            string LastLineText = rtb1.Lines[rtb1.Lines.Count() - 1];
            int LastLineStartIndex = rtb1.Text.LastIndexOf(LastLineText);
            // Set selection
            rtb1.SelectionStart = LastLineStartIndex;
            rtb1.SelectionLength = rtb1.Text.Length;
            // Change colour
            rtb1.SelectionColor = fgColour;
        }
    }
于 2015-09-02T22:25:02.787 回答