habbo95
habbo95

Reputation: 43

richtextbox font

I want to change the font color and size for 1 line in richTextBox

   String [] wo = {"hi","hello","11111","he","she"};
   richTextBox1.SelectionFont = new Font("Verdana", 10, FontStyle.Regular);
   richTextBox1.SelectionColor = Color.Blue;
   richTextBox1.SelectedText += Environment.NewLine + wo[0];
   richTextBox1.SelectedText += Environment.NewLine + wo[1];              
   richTextBox1.SelectedText += Environment.NewLine + wo[2];
   richTextBox1.SelectedText += Environment.NewLine + wo[3];
   richTextBox1.SelectedText += Environment.NewLine + wo[4];

I want to change just the string "11111" and keep the rest lines as default any help

Upvotes: 0

Views: 2350

Answers (2)

Patrick
Patrick

Reputation: 17973

This should work

private static void setColorOnLine(RichTextBox richTextBox1, int line, Color col) {
    // save old values
    int caretPosition = richTextBox1.SelectionStart;
    int selectionLength = richTextBox1.SelectionLength;
    Color selectionColor = richTextBox1.SelectionColor;

    int start = richTextBox1.GetFirstCharIndexFromLine(line);
    int count = richTextBox1.Lines[line].Length;
    richTextBox1.Select(start, count);
    richTextBox1.SelectionColor = col;

    // restore
    richTextBox1.SelectionStart = caretPosition;
    richTextBox1.SelectionLength = selectionLength;
    richTextBox1.SelectionColor = selectionColor;
}

Upvotes: 1

Hans Olsson
Hans Olsson

Reputation: 55001

You can use the Select method to select the row (via text positions) and then you use the properties SelectionColor and SelectionFont to change the settings.

You can use the Find method to find the text to change.

Here's the help page for Select:

http://msdn.microsoft.com/en-us/library/xc4fh98s.aspx

Upvotes: 0

Related Questions