RichTextBox, append a text string and high-light portions of the string that don\'t match a reference string.


/ Published in: C#
Save to your folder(s)

This static function will append a string to a RichTextBox while high-lighting characters that don't match a reference string. It's similar to a file-compare GUI that high-lights mis-matched characters.

![alt text](https://docs.google.com/uc?id=0Bw1KoEBfCFrEMmY1ZmVjZjUtMGE4Ny00OWE0LTg1YWItNDkzNzljMWZiYTA2&export=download&authkey=CNOQvbwI&hl=en)


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Append text to a RichTextBox and highlight characters that don't match the reference string.
  3. /// The highlight color is hard-coded as Red. This function goes thru the txtArg char-by-char
  4. /// and sets chars Red if they don't match.
  5. /// </summary>
  6. /// <param name="rtbArg">A RichTextBox</param>
  7. /// <param name="txtArg">Text to append. Characters that don't match the reference string are highlighted.</param>
  8. /// <param name="refArg">Reference string.</param>
  9. static void AppendStringDiff(RichTextBox rtbArg, string txtArg, string refArg)
  10. {
  11. rtbArg.AppendText(((rtbArg.Text.Length > 0) ? "\n" : string.Empty)); // Append newline.
  12.  
  13. int beg = rtbArg.Text.Length; // Keep track of where we're starting.
  14.  
  15. int maxLen = Math.Max(txtArg.Length, refArg.Length); // handles mis-matched lengths.
  16.  
  17. rtbArg.AppendText(txtArg + new string(' ', maxLen - txtArg.Length));
  18.  
  19. bool matchPrev=false; // remember last match state.
  20.  
  21. for (int i = 0; i <= maxLen; i++)
  22. {
  23. // Get chars in the txt string and the reference string.
  24. // If we're past the string's end, use '\0'
  25. char refChar = i < refArg.Length ? refArg[i] : '\0';
  26. char txtChar = i < txtArg.Length ? txtArg[i] : '\0';
  27.  
  28. bool misMatch = refChar != txtChar; // chars different?
  29.  
  30. if (matchPrev && !misMatch) // If chars match again or at end of string, get color.
  31. {
  32. rtbArg.SelectionLength = beg+i - rtbArg.SelectionStart;
  33. rtbArg.SelectionBackColor = Color.Yellow;
  34. }
  35.  
  36. if (i == maxLen) // at end of string.
  37. {
  38. break;
  39. }
  40.  
  41. if (misMatch && !matchPrev) // if chars don't match, mark the beginning of Red.
  42. {
  43. rtbArg.SelectionStart = beg+i;
  44. }
  45. matchPrev = misMatch; // remember match-state.
  46. } // for
  47. rtbArg.SelectionLength = 0;
  48. }
  49.  
  50. /// <summary>
  51. /// Example usage and testing.
  52. /// Capital letters will appear high-lighted because they don't match the reference string.
  53. /// </summary>
  54. void AppendStringDiff_Test()
  55. {
  56. AppendStringDiff(richTextBox1, "XaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaX" , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  57. AppendStringDiff(richTextBox1, "bbbbbb THIS STRING IS LONG bbbbbbbbbbbbbbbbbbbbbbbb", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  58. AppendStringDiff(richTextBox1, "ccc THIS STRING IS SHORT ccccccccccccccccccccccc" , "cccccccccccccccccccccccccccccccccccccccccccccccccc");
  59. AppendStringDiff(richTextBox1, "dddddddddddddddddddddddddddddddddddddddddddddddddd" , "dddddddddddddddddddddddddddddddddddddddddddddddddd");
  60. AppendStringDiff(richTextBox1, "eEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeE" , "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
  61. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.