【问题标题】:RichTextBox latest lineRichTextBox 最新行
【发布时间】: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


【解决方案1】:

这对你有用:

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,这将允许保留现有颜色。

【讨论】:

  • 为什么要使用首字母大写的参数名...看到这样写的 C# 代码非常令人困惑。
  • 嗯,它肯定会以正确的方式推动我:),还有一个问题:当我只想更改一行中的 1 个单词时,我该怎么做,假设该行包含以下内容: This is a phone 那么我想让phone这个词是红色的,我该怎么做呢?并且还保留文本框中的颜色?
  • 您必须处理SelectionStartSelectionLength 来定义颜色变化将发生的位置。尝试一下!这是学习的唯一方法...尝试在SelectionStart 中添加一个数字并将SelectionLength 设置为一个固定数字。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多