richtextbox字体

时间:2010-05-22 18:12:13

标签: c# winforms

我想在richTextBox

中更改1行的字体颜色和大小
   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];

我想只更改字符串“11111”并将其余行保留为默认值 任何帮助

2 个答案:

答案 0 :(得分:1)

这应该有效

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

答案 1 :(得分:0)

您可以使用Select方法选择行(通过文字位置),然后使用属性SelectionColorSelectionFont更改设置。

您可以使用Find方法查找要更改的文字。

以下是Select的帮助页面:

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