• 12
• 11
• 9
• 10
• 13

# [.net] How does the WinMerge/WinDiff works and how to give highlight color to a block of tex

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to develop a text comparer similar to WinMerge or WinDiff. 1. I'd like to know how WinDiff / WinMergeworks to compare 2 files and found the difference. 2. How to give highlight color to text blocks which is differ each other? I just need to show the difference between 2 files and mark them with highlight color. So not all WinDiff features I need. Thank you very much..

##### Share on other sites
On a very basic level you can have two textboxes.

Loop through the text in textbox1.Text a character at a time and compare it to textbox2.Text. Everytime textbox1.Text != textbox2.Text then hilight the text.

Most text comparing software allows you to adjust the offset it checks. So you can have textbox1.Text != textbox2.Text[i + Offset] to hilight. Offset can be a variable that can be set using buttons in your GUI to Offset-- or Offset++.

Finally to have the hilighting you need you will need to use richtextbox's instead of textboxes.

To hilight a character that is different use the following:

for(int i=0; i<richTextBox1.Text.Length && i<richTextBox2.Text.Length; i++){   if(richTextBox1.Text != richTextBox2.Text)   {      richTextBox1.SelectionStart = i;      richTextBox1.SelectionLength = 1;      richTextBox1.SelectionColor = Color.Red;      richTextBox2.SelectionStart = i;      richTextBox2.SelectionLength = 1;      richTextBox2.SelectionColor = Color.Red;   }}

##### Share on other sites
Ok, thank you. I'll try it first..

##### Share on other sites
Wikipedia's article on the longest common subsequence problem presents a pseudo-code algorithm that can be used to calculate the 'diff' between two files.

It uses a recursive method; you'll need to convert it to an iterative method if you wish to avoid StackOverflowExceptions. [smile]

##### Share on other sites
Is it enough to use only the algorithm you gave me to check a very long text file?

I also have other problem which is how to display unicode characters? I need to display Japanese Kanji, Hiragana, Katakana, Chinese, Korean and Thai characters in the proper way..

I have tried this way:

but it still cannot display the text file containing Japanese Kanji / Hiragana / Katakana well and WORSE, a text file containing Latin characters also displayed as Asian characters but I don't know what characters they were.

Can you please tell me how to display all text file well?

Thank you very much.