code_junkie

Selectively coloring text in RichTextBox

Nov 13th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. static void HighlightPhrase(RichTextBox box, string phrase, Color color) {
  2. int pos = box.SelectionStart;
  3. string s = box.Text;
  4. for (int ix = 0; ; ) {
  5. int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
  6. if (jx < 0) break;
  7. box.SelectionStart = jx;
  8. box.SelectionLength = phrase.Length;
  9. box.SelectionColor = color;
  10. ix = jx + 1;
  11. }
  12. box.SelectionStart = pos;
  13. box.SelectionLength = 0;
  14. }
  15.  
  16. private void button1_Click(object sender, EventArgs e) {
  17. richTextBox1.Text = "Aardvarks are strange animals";
  18. HighlightPhrase(richTextBox1, "a", Color.Red);
  19. }
  20.  
  21. Function Highlight(ByVal Search_Str As Object, ByVal InputTxt As String, ByVal StartTag As String, ByVal EndTag As String) As String
  22. Highlight = Regex.Replace(InputTxt, "(" & Regex.Escape(Search_Str) & ")", StartTag & "$1" & EndTag, RegexOptions.IgnoreCase)
  23. End Function
  24.  
  25. public string Highlight(object Search_Str, string InputTxt, string StartTag, string EndTag)
  26. {
  27. return Regex.Replace(InputTxt, "(" + Regex.Escape(Search_Str) + ")", StartTag + "$1" + EndTag, RegexOptions.IgnoreCase);
  28. }
  29.  
  30. public string Highlight(object Search_Str, string InputTxt, string StartTag, string EndTag)
  31. {
  32. return Regex.Replace(InputTxt, "(" + Regex.Escape(Search_Str) + ")", StartTag + "$1" + EndTag, RegexOptions.IgnoreCase);
  33. }
  34.  
  35. public string Highlight(object Search_Str, string InputTxt, string StartTag, string EndTag)
  36. {
  37. return Regex.Replace(InputTxt, "(" + Regex.Escape(Search_Str) + ")", StartTag + "$1" + EndTag, RegexOptions.IgnoreCase);
  38. }
  39.  
  40. private delegate void AddMessageCallback(string message, Color color);
  41.  
  42. public void AddMessage(string message)
  43. {
  44. Color color = Color.Empty;
  45.  
  46. string searchedString = message.ToLowerInvariant();
  47.  
  48. if (searchedString.Contains("failed")
  49. || searchedString.Contains("error")
  50. || searchedString.Contains("warning"))
  51. {
  52. color = Color.Red;
  53. }
  54. else if (searchedString.Contains("success"))
  55. {
  56. color = Color.Green;
  57. }
  58.  
  59. AddMessage(message, color);
  60. }
  61.  
  62. public void AddMessage(string message, Color color)
  63. {
  64. if (_richTextBox.InvokeRequired)
  65. {
  66. AddMessageCallback cb = new AddMessageCallback(AddMessageInternal);
  67. _richTextBox.BeginInvoke(cb, message, color);
  68. }
  69. else
  70. {
  71. AddMessageInternal(message, color);
  72. }
  73. }
  74.  
  75. private void AddMessageInternal(string message, Color color)
  76. {
  77. string formattedMessage = String.Format("{0:G} {1}{2}", DateTime.Now, message, Environment.NewLine);
  78.  
  79. if (color != Color.Empty)
  80. {
  81. _richTextBox.SelectionColor = color;
  82. }
  83. _richTextBox.SelectedText = formattedMessage;
  84.  
  85. _richTextBox.SelectionStart = _richTextBox.Text.Length;
  86. _richTextBox.ScrollToCaret();
  87. }
Add Comment
Please, Sign In to add comment