如何在RichTextBox中更改特定行的背景颜色?

7
我想要改变整行的颜色,无论该行是否有文本。以下是一些解释性图片: http://img131.imageshack.us/img131/1802/highlightlineqt2.png
我在这里找到了一些解决方案(链接),但我希望有一个更简单的解决方案。

对于这个很棒的插图点赞 +1 :] - Rasmus Søborg
3个回答

2
不,你首先必须选择该行,然后设置颜色。
 public void MarkSingleLine()
 {
     int firstCharOfLineIndex = myRichTextBox.GetFirstCharIndexOfCurrentLine();
     int currentLine = richTextBox1.GetLineFromCharIndex(firstCharOfLineIndex);
     this.myRichTextBox.Select(firstCharOfLineIndex, currentLine);
     this.myRichTextBox.SelectionBackColor = Color.Aqua;
     this.myRichTextBox.Select(0, 0);
 }

0

好的,那么也许这个链接(在这里找到)可以帮到你:

private void richTextBox1_MouseClick(object sender, MouseEventArgs e, Color color)
{
    int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();
    int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);
    string currentlinetext = richTextBox1.Lines[currentline];
    richTextBox1.SelectionBackColor = color;
    richTextBox1.Select(firstcharindex, currentlinetext.Length);
}

这段代码片段应该可以解决你的问题 ;-)

0
你可以使用这段代码:
private void richTextBox_LOG_write_text(string text, Color text_color, Color background_color)
    {
        try
        {
            if(richTextBox_LOG.InvokeRequired == true)
            {
                Invoke(new Delegate_void_string_colortext_colorbackground(richTextBox_LOG_write_text), new object[] { text, text_color, background_color  });
            }
            int text_size = richTextBox_LOG.Text.Length;
            richTextBox_LOG.AppendText(text);
            richTextBox_LOG.Select(text_size, text.Length);
            if(text_color == null)
            {
                text_color = Color.Black;
            }
            richTextBox_LOG.SelectionColor = text_color;
            if(background_color != null)
            {
                richTextBox_LOG.SelectionBackColor = background_color;
            }
        }
        catch { }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接