2010-05-22 56 views
0

我想改變字體顏色和大小的1號線在RichTextBox中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]; 

我想改變只是字符串「11111」,並保持休息線爲默認 任何幫助

+0

,做你想保持/恢復的選擇? – 2010-05-22 18:58:59

+0

是的,只需將wo [2]更改爲藍色,並將其餘的保存爲默認 – habbo95 2010-05-22 19:02:17

回答

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

謝謝你,它確實有效 – habbo95 2010-05-22 19:51:42

0

您可以使用Select方法選擇行(通過文本位置),然後使用屬性SelectionColorSelectionFont更改設置。

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

下面是Select幫助頁面:

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