Tenere
Tenere

Reputation: 371

How to change background color of the certain line in the RichTextBox?

I want to change the color of the entire line, regardless of whether the text is there is or no. Here is some explaining image:

http://img131.imageshack.us/img131/1802/highlightlineqt2.png.

I found some solution here, but I hope that there is a simpler solution.

Upvotes: 7

Views: 7891

Answers (3)

You can use this piece of code:

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

Upvotes: 0

Anonym
Anonym

Reputation: 21

No, you first have to select the line, then you have to set the color:

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

Upvotes: 2

jwillmer
jwillmer

Reputation: 3789

o.k., then maybe this (found here):

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

this snippet should solve your problem ;-)

Upvotes: 0

Related Questions