2009-01-18 81 views
6

我怎樣才能在紅色每當我滿足RichTextBox的字母「A」的時間畫畫嗎?選擇性着色文本RichTextBox中

+0

[一個RichTextBox串的顏色不同部分(HTTPS的可能重複:// stackoverflow.com/questions/1926264/color-different-parts-of-a-richtextbox-string) – 2017-06-01 17:45:12

回答

26

試試這個:

static void HighlightPhrase(RichTextBox box, string phrase, Color color) { 
    int pos = box.SelectionStart; 
    string s = box.Text; 
    for (int ix = 0; ;) { 
    int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase); 
    if (jx < 0) break; 
    box.SelectionStart = jx; 
    box.SelectionLength = phrase.Length; 
    box.SelectionColor = color; 
    ix = jx + 1; 
    } 
    box.SelectionStart = pos; 
    box.SelectionLength = 0; 
} 

...

private void button1_Click(object sender, EventArgs e) { 
    richTextBox1.Text = "Aardvarks are strange animals"; 
    HighlightPhrase(richTextBox1, "a", Color.Red); 
} 
1

,而你是打字,如果這是你在找什麼這不會工作,但我用這個來突出子:

Function Highlight(ByVal Search_Str As Object, ByVal InputTxt As String, ByVal StartTag As String, ByVal EndTag As String) As String 
    Highlight = Regex.Replace(InputTxt, "(" & Regex.Escape(Search_Str) & ")", StartTag & "$1" & EndTag, RegexOptions.IgnoreCase) 
End Function 

,並調用它是這樣的:

 
Highlight("A", "Color All my A's red", [span class=highlight]', '[/span]') 

其中班級'突出'具有任何顏色編碼/格式你想要的:

 
.highlight {text-decoration: none;color:black;background:red;} 

順便說一句:你需要改變th OSE方括號彎角的...他們不會來了直通當我鍵入他們...

+0

感謝對幫助,但你有沒有C#代碼? – Gold 2009-01-18 19:32:38

1

這是C#爲EJ布倫南的答案代碼:

public string Highlight(object Search_Str, string InputTxt, string StartTag, string EndTag) 
{ 
    return Regex.Replace(InputTxt, "(" + Regex.Escape(Search_Str) + ")", StartTag + "$1" + EndTag, RegexOptions.IgnoreCase); 
} 
2

這裏是一個片段從我的包裝類的做這個工作:

private delegate void AddMessageCallback(string message, Color color); 

    public void AddMessage(string message) 
    { 
     Color color = Color.Empty; 

     string searchedString = message.ToLowerInvariant(); 

     if (searchedString.Contains("failed") 
      || searchedString.Contains("error") 
      || searchedString.Contains("warning")) 
     { 
      color = Color.Red; 
     } 
     else if (searchedString.Contains("success")) 
     { 
      color = Color.Green; 
     } 

     AddMessage(message, color); 
    } 

    public void AddMessage(string message, Color color) 
    { 
     if (_richTextBox.InvokeRequired) 
     { 
      AddMessageCallback cb = new AddMessageCallback(AddMessageInternal); 
      _richTextBox.BeginInvoke(cb, message, color); 
     } 
     else 
     { 
      AddMessageInternal(message, color); 
     } 
    } 

    private void AddMessageInternal(string message, Color color) 
    { 
     string formattedMessage = String.Format("{0:G} {1}{2}", DateTime.Now, message, Environment.NewLine); 

     if (color != Color.Empty) 
     { 
      _richTextBox.SelectionColor = color; 
     } 
     _richTextBox.SelectedText = formattedMessage; 

     _richTextBox.SelectionStart = _richTextBox.Text.Length; 
     _richTextBox.ScrollToCaret(); 
    } 

現在您可以使用AddMessage("The command failed")來調用它,以紅色自動突出顯示。或者你可以用AddMessage("Just a special message", Color.Purple)調用它來定義一個特殊的顏色(catch塊內有用例如定義一個特定的顏色,無論郵件內容)

+0

感謝奧利弗,你的代碼幫我! – Maro 2014-03-03 14:38:07