Revision: 43089
Updated Code
at March 16, 2011 14:38 by jimfred
Updated Code
/// <summary> /// Append text to a RichTextBox and highlight characters that don't match the reference string. /// The highlight color is hard-coded as Red. This function goes thru the txtArg char-by-char /// and sets chars Red if they don't match. /// </summary> /// <param name="rtbArg">A RichTextBox</param> /// <param name="txtArg">Text to append. Characters that don't match the reference string are highlighted.</param> /// <param name="refArg">Reference string.</param> static void AppendStringDiff(RichTextBox rtbArg, string txtArg, string refArg) { rtbArg.AppendText(((rtbArg.Text.Length > 0) ? "\n" : string.Empty)); // Append newline. int beg = rtbArg.Text.Length; // Keep track of where we're starting. int maxLen = Math.Max(txtArg.Length, refArg.Length); // handles mis-matched lengths. rtbArg.AppendText(txtArg + new string(' ', maxLen - txtArg.Length)); bool matchPrev=false; // remember last match state. for (int i = 0; i <= maxLen; i++) { // Get chars in the txt string and the reference string. // If we're past the string's end, use '\0' char refChar = i < refArg.Length ? refArg[i] : '\0'; char txtChar = i < txtArg.Length ? txtArg[i] : '\0'; bool misMatch = refChar != txtChar; // chars different? if (matchPrev && !misMatch) // If chars match again or at end of string, get color. { rtbArg.SelectionLength = beg+i - rtbArg.SelectionStart; rtbArg.SelectionBackColor = Color.Yellow; } if (i == maxLen) // at end of string. { break; } if (misMatch && !matchPrev) // if chars don't match, mark the beginning of Red. { rtbArg.SelectionStart = beg+i; } matchPrev = misMatch; // remember match-state. } // for rtbArg.SelectionLength = 0; } /// <summary> /// Example usage and testing. /// Capital letters will appear high-lighted because they don't match the reference string. /// </summary> void AppendStringDiff_Test() { AppendStringDiff(richTextBox1, "XaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaX" , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); AppendStringDiff(richTextBox1, "bbbbbb THIS STRING IS LONG bbbbbbbbbbbbbbbbbbbbbbbb", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); AppendStringDiff(richTextBox1, "ccc THIS STRING IS SHORT ccccccccccccccccccccccc" , "cccccccccccccccccccccccccccccccccccccccccccccccccc"); AppendStringDiff(richTextBox1, "dddddddddddddddddddddddddddddddddddddddddddddddddd" , "dddddddddddddddddddddddddddddddddddddddddddddddddd"); AppendStringDiff(richTextBox1, "eEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeEeE" , "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); }
Revision: 43088
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 16, 2011 14:34 by jimfred
Initial Code
a
Initial URL
Initial Description
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)
Initial Title
RichTextBox, append a text string and high-light portions of the string that don\'t match a reference string.
Initial Tags
Initial Language
C#