如何比较两个丰富的文本框的内容,并突出显示更改的人物? [英] How to compare two rich text box contents and highlight the characters that are changed?

查看:470
本文介绍了如何比较两个丰富的文本框的内容,并突出显示更改的人物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code,我用于读取2 RichTextBox的内容如下:

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

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

现在,我需要比较两个丰富的文本框的内容,并突出显示在两个richtextboxes改变的字符。目的是通过C#应用程序,以获得差异,突出了人物在TFS (比较文件)。谢谢你。

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.

编辑:

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;
   }
}

我从调试的理解是,选择 selectionColor设置的SelectionBackColor richTextBox1指向文本光标<强法>增加至7个位置执行特定的行之后。如何保持光标位置 richTextBox1的?

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 ?

推荐答案

首先荣誉给ArtyomZzz为指向DiffMatchPatch的重要来源!

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

下面是一块code中的将在一个按钮,单击画图更改字符两个RichTextboxes的。

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

首先下载差异匹配-patchsource 。从ZIP文件拷贝'DiffMatchPatch.cs,也复制到您的项目,并inlude的CS文件中你的项目。同样的命名空间添加到您的使用条款。

First download the diff-match-patchsource. 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..

注:有四个不同的后差异清除方法。这是最适合将取决于输入和目的。我建议您尝试了 cleanupSemanticLossless 。我添加了一个第三截图显示如何清理工作

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:

和新版本之一:

And one of the new version:

cleanupSemanticLossless后截图:

Screenshot after cleanupSemanticLossless:

这篇关于如何比较两个丰富的文本框的内容,并突出显示更改的人物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆