8

Code that I used for reading the 2 richtextbox contents are as follows:

richTextBox1.Text = File.ReadAllText(tfsVersionFilePath);
richTextBox2.Text = File.ReadAllText(dbVersionFilePath);

Now, I need to compare the two rich text box contents and highlight the characters that are changed in both richtextboxes. Purpose is to get the difference and highlight the characters as in TFS(comparing files) through c# application. Thanks.

Edit:

int length = (richTextBox1.Text.Length > richTextBox2.Text.Length) ? richTextBox1.Text.Length : richTextBox2.Text.Length;
for (int i = 0; i < length; i++)
{ 
   if (richTextBox1.Text[i] != richTextBox2.Text[i])
   {
      /* and then start your highlight selection here, 
      this is where some difference between the two rich 
      text boxes begins */

      richTextBox1.Select(i, 1); 
      richTextBox1.SelectionColor = System.Drawing.Color.Yellow; 
      richTextBox1.SelectionBackColor = System.Drawing.Color.Red;
   }
}

What I understood from debugging is that the Select or SelectionColor or SelectionBackColor method of richTextBox1 pointing the text cursor increased to 7 positions after the particular lines executed. How to maintain the cursor position of richTextBox1 ?

Earth
  • 3,477
  • 6
  • 37
  • 78
  • "highlight the characters that are changed " How characters ware changed while comparing? – Suji Jul 22 '14 at 12:23
  • do you want to check word by word or letter by letter? – Suji Jul 22 '14 at 12:24
  • Latest Version in one richtextbox and previous version in another rich text box. As in TFS even if 50 changed to 60, then it will highlight `5` and `6` in both versions.Likewise, Sujith. Thanks. – Earth Jul 22 '14 at 12:24
  • okey then what will be the expected output? – Suji Jul 22 '14 at 12:27
  • @SujithKarivelil, Expected output at this point, I need to highlight those changed characters `and/or` words. Thanks. – Earth Jul 22 '14 at 12:28
  • here you want to highlight "**Latest & previous**",isn't it? – Suji Jul 22 '14 at 12:30
  • @SujithKarivelil, Yes, Exactly. – Earth Jul 22 '14 at 12:30
  • Take a look at http://stackoverflow.com/questions/1509771/text-comparison-difference-algorithm – Kris Vandermotten Jul 25 '14 at 06:14
  • @KrisVandermotten, I am not able to run the application from the source code that has been provided in the above link `diff.net`. Throwing lot of errors, nearly tried the source code to run for atleast 3 hours. But with the app(installer) in the downloaded folder, I tried and it gives me the expected result and the differences displaying was fine. Now, I don't know how to proceed, Thanks. – Earth Jul 25 '14 at 09:29

3 Answers3

21

First kudos to ArtyomZzz for pointing to the great source of DiffMatchPatch!

Here is a piece of code the will paint the changed characters in two RichTextboxes upon a button click.

First download the diff-match-patchsource. (!See update below!) From the zip file copy 'DiffMatchPatch.cs' and also 'COPY' to your project and inlude the cs file in you project. Also add the namespace to your using clauses.

using DiffMatchPatch;

namespace RTF_diff
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    // this is the diff object;
    diff_match_patch DIFF = new diff_match_patch();

    // these are the diffs
    List<Diff> diffs;

    // chunks for formatting the two RTBs:
    List<Chunk> chunklist1; 
    List<Chunk> chunklist2;

    // two color lists:
    Color[] colors1 = new Color[3] { Color.LightGreen, Color.LightSalmon, Color.White };
    Color[] colors2 = new Color[3] { Color.LightSalmon, Color.LightGreen, Color.White };


    public struct Chunk
    {
        public int startpos;
        public int length;
        public Color BackColor;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        diffs = DIFF.diff_main(RTB1.Text, RTB2.Text);
        DIFF.diff_cleanupSemanticLossless(diffs);      // <--- see note !

        chunklist1 = collectChunks(RTB1);
        chunklist2 = collectChunks(RTB2);

        paintChunks(RTB1, chunklist1);
        paintChunks(RTB2, chunklist2);

        RTB1.SelectionLength = 0;
        RTB2.SelectionLength = 0;
    }


    List<Chunk> collectChunks(RichTextBox RTB)
    {
        RTB.Text = "";
        List<Chunk> chunkList = new List<Chunk>();
        foreach (Diff d in diffs)
        {
            if (RTB == RTB2 && d.operation == Operation.DELETE) continue;  // **
            if (RTB == RTB1 && d.operation == Operation.INSERT) continue;  // **

            Chunk ch = new Chunk();
            int length = RTB.TextLength;
            RTB.AppendText(d.text);
            ch.startpos = length;
            ch.length = d.text.Length;
            ch.BackColor = RTB == RTB1 ? colors1[(int)d.operation]
                                       : colors2[(int)d.operation];
            chunkList.Add(ch);
        }
        return chunkList;

    }

    void paintChunks(RichTextBox RTB, List<Chunk> theChunks)
    {
        foreach (Chunk ch in theChunks)
        {
            RTB.Select(ch.startpos, ch.length);
            RTB.SelectionBackColor = ch.BackColor;
        }

    }

  }
}

At first I was trying to also color the changed lines as a whole in a lighter color; however that takes considerably more work, can't be done (coloring the whole line as opposed to just the part with content), and was not part of your question in the first place..

Note: There are four different post-diff cleanup methods. Which is best suited will depend on the input and purpose. I suggest trying the cleanupSemanticLossless. I have added a 3rd screenshot showing how this cleanup works

Here is a screenshot of the output: original output

And one of the new version: new screenshot

Screenshot after cleanupSemanticLossless: 3rd screenshot

Update: The sources seem to have moved. This should help..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thank You for your answer. I will try your solution and update you once I completed. Thanks. – Earth Jul 26 '14 at 15:53
  • Above screenshot in your answer is having the same text on both `richtextboxes` and **no differences** present. So, there should not be any highlight but it is **showing the highlight** on some texts. Please explain. Thanks. – Earth Jul 27 '14 at 07:06
  • Also, what is the namespace that we need to add for the `Chunk` in `List chunklist1;` – Earth Jul 27 '14 at 07:20
  • Green parts are added red parts omitted compared to the other text. in the balance this makes the texts the same. I thought that would be fine, but now I see that one side effect is that one can't keep working with the texts which now are identical. Pressing the Diff button twice removes all highlights. Outch! I'll adapt my answer soon.. – TaW Jul 27 '14 at 07:20
  • using DiffMatchPatch; – TaW Jul 27 '14 at 07:21
  • Ok, I will be waiting for your updated answer. Thanks. – Earth Jul 27 '14 at 07:22
  • Already I used `DiffMatchPatch`, and I am able to use `Diff` through that but `chunk` it is asking for the reference. Thanks. – Earth Jul 27 '14 at 07:24
  • Added two lines ( ** ) to suppress chunks that actually are not in the Text. This makes the color scheme redundant since nothing deleted is ever shown, of course.. Chunk is a structure defined in the code. Did you add the DiffMatchPatch.cs to your project? – TaW Jul 27 '14 at 07:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58093/discussion-between-taw-and-john-stephen). – TaW Jul 27 '14 at 07:44
  • I tried your updated solution. It was working fine as expected. Thank You very much for your valuable time spending with me and for your great answer. Thanks a lot. – Earth Jul 27 '14 at 09:45
  • Google Code seems to be buggy as hell: All files are removed, and even some links like _browse" return errors... – Lion Mar 11 '17 at 13:08
  • Hm, not sure about this: [looks ok, but I ddin't test](https://code.google.com/p/google-diff-match-patch/downloads/detail?name=diff_match_patch_20121119.zip&can=2&q=) – TaW Mar 11 '17 at 13:20
  • Sorry. The last sentence in your answer says `This or this should help..`. Both `this` point to the same link. Is it a typo? – Second Person Shooter Apr 11 '18 at 17:39
  • Hm, I don't quite remember, but you are right, both point to the same github page. I'll delete one.. – TaW Apr 11 '18 at 17:43
  • You say: "Added two lines ( ** ) to suppress chunks that actually are not in the Text. .... " This way the line syncronization is lost, cause the eventual new line ('\n') is not applied. – Caesar Jul 21 '18 at 16:40
  • for c# wpf, how do i replace the rtb.selectionlength, rtb.textlength,rtb.select and rtb.selectonbackcolor property? –  Nov 27 '19 at 06:03
  • @John: I don't do WPF. You need to study the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.richtextbox.selection?view=netframework-4.8) and/or look for a [tutorial](https://www.wpf-tutorial.com/rich-text-controls/richtextbox-control/); there are a few out there.. – TaW Nov 27 '19 at 08:33
  • Does anyone have an example of this using VB.Net? – Michael Harmon Jun 14 '21 at 18:42
3

As I understand problem is:

  • compare 2 files
  • show differences in files in .net app.

The simplest way is using of https://github.com/curran/google-diff-match-patch (previous link was https://code.google.com/p/google-diff-match-patch/, thanks to Paolo Costa for comment)

It can compare files (with several options that you can set) and form Html for you (with diffs). Also you can write your own logic for outputting (if html isn't suiting for you) - see DiffMatchPatch.diff_prettyHtml(...) method (it's very simple).

p.s.

if (richTextBox1.Text[i] != richTextBox2.Text[i])

isn't "best" approach for file comparison. It's more complex.

Community
  • 1
  • 1
Artyom
  • 114
  • 4
  • `google diff` I already saw the demo they provided. It doesn't show the differences as `TFS` compare files and it is displaying the differences below. Also, can you suggest me a good approach for file comparision. Thanks. – Earth Jul 25 '14 at 12:03
  • Can you provide 2 simple files that you are comparing and screenshot of expected result? I'm not using TFS (only git) but I think that they are very similar, and google lib can reproduce behavior of TFS. – Artyom Jul 25 '14 at 12:52
  • Thank You for your reply and suggesting to use `google diff` and I got the expected result to display differences and highlight as followed by TaW's answer. – Earth Jul 27 '14 at 09:51
0

Just thinking aloud here, but another way to do it would be :

int length = (richTextBox1.Text.Length > richTextBox2.Text.Length) ? richTextBox1.Text.Length : richTextBox2.Text.Length;
for (int i = 0; i < length; i++)
{ 
   if (richTextBox1.Text[i] != richTextBox2.Text[i])
   {
      /* and then start your highlight selection here, 
      this is where some difference between the two rich 
      text boxes begins */
   }

This would traverse both rich text boxes at the same time and highlight the differences. Sounds like it may be more of what you're looking for.

Use the link I posted from my other answer to help you along with the highlighting bit. This little piece of code should help with the text comparison.

  • I am able to get the difference, but highlighting is working wrongly.(i.e) seven characters increased and pointing. – Earth Jul 23 '14 at 06:28
  • @JohnStephen it should work, can you post the piece which is causing you an issue? May be something simple, – Michael Di Felice Jul 23 '14 at 12:19
  • The code that I added is inside the `if` condition which was as follows `richTextBox1.Select(i, 1); richTextBox1.SelectionColor = System.Drawing.Color.Yellow; richTextBox1.SelectionBackColor = System.Drawing.Color.Red;` – Earth Jul 24 '14 at 05:27
  • What I understood from debugging is that the `Select` or `SelectionColor` or `SelectionBackColor` method of `richTextBox1` pointing the text cursor increased to 7 positions after the particular lines executed.. – Earth Jul 24 '14 at 05:45
  • You may have more luck with the following example, using a marker to determine the selection start and then moving forward the number of characters that remain different as you parse the richTextBox text : http://stackoverflow.com/questions/14765947/highlight-specific-text-in-richtextbox – Michael Di Felice Jul 24 '14 at 15:03
  • I refer the link, but not able to understand regarding what to do for my issue with those answers. Can you provide an update in your answer from that link. Thanks. – Earth Jul 25 '14 at 05:27