Coffeehouse Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Comparing two TextBoxes in C# windows forms

Back to Forum: Coffeehouse
  • User profile image
    rezaElc87

    Hi to all!

    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!!

     

  • User profile image
    evildictait​or

    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.

  • User profile image
    rezaElc87

    @evildictaitor: Thanks for you attention.

    but I really need to do the job. What if I used a rich text box? Will I be able to do as I said? 

  • User profile image
    JoshRoss

    @rezaElc87: Take some initiative! That was really good advice you received from Evil D.

  • User profile image
    JohnAskew

    RichTextBoxes allow for coloring, highlighting, etc. It will provide capability for the display you desire.

    How to compare files? Careful read evildictaitor's post. The information is there for you to discover, learn, and choose between.

    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.

  • User profile image
    cbae

    http://winmerge.org/downloads/source-code.php

    Why reinvent the wheel?

    Edit: Oops. GPLv2. Sorry. Can't do much with this source code other than to learn some basic concepts from it.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.