I am trying to write a code in windows forms in c# to open two text files and compare them.
the process is as follows:
1- two text boxes are provided on the window
2- the files are loaded into text boxes using OpenFileDialogue
3- I want to have the first text as the base and compare the second one to that and highlight the similarities and differences using different colors on the second text.
I actually don't know how to do step 3.
many thanks for your helps!!
You cannot highlight different parts of a textbox different colours, so you will need to think of a different approach to achieve your goal.
If I were you, I'd try breaking this problem into two; firstly there's the functional problem: "Given two strings, how can I describe how they are different". This isn't necessarilly obvious, but look at something like the levenstein distance algorithm and you might get some pointers as to where to start or google for algorithms for "diff"-ing strings.
The second problem is one of rendering the output: "Given a description of the differences between two strings (in whatever format my program provides it to me), put this on the screen in a visible way". One way you might want to consider is using a RichTextBox or outputting HTML directly into a WebBrowser control.
Decouple these. That way you will only have two self contained and moderately hard problems to solve instead of one sprawling and really hard problem to solve. When you've finished the first problem, feed it's output into the input of the second and you'll have your total solution.
Edit: Oops. GPLv2. Sorry. Can't do much with this source code other than to learn some basic concepts from it.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.