C# 谢谢你的回答。我将尝试您的解决方案,并在完成后更新您。谢谢。您答案中的上述屏幕截图在两个richtextbox上显示的文本相同,没有差异。因此,不应该有任何突出显示,但它显示了一些文本上的突出显示。请解释一下。谢谢。另外,我们需要为列表chunklist1

C# 谢谢你的回答。我将尝试您的解决方案,并在完成后更新您。谢谢。您答案中的上述屏幕截图在两个richtextbox上显示的文本相同,没有差异。因此,不应该有任何突出显示,但它显示了一些文本上的突出显示。请解释一下。谢谢。另外,我们需要为列表chunklist1,c#,.net,winforms,richtextbox,C#,.net,Winforms,Richtextbox,谢谢你的回答。我将尝试您的解决方案,并在完成后更新您。谢谢。您答案中的上述屏幕截图在两个richtextbox上显示的文本相同,没有差异。因此,不应该有任何突出显示,但它显示了一些文本上的突出显示。请解释一下。谢谢。另外,我们需要为列表chunklist1中的Chunk添加什么名称空间绿色部分被添加,与其他文本相比,红色部分被省略。在平衡中,这使得文本相同。我原以为这样就可以了,但现在我看到了一个副作用,就是人们不能继续使用现在相同的文本。按两次Diff按钮可删除所有高光。走开!我将很快调整我的


谢谢你的回答。我将尝试您的解决方案,并在完成后更新您。谢谢。您答案中的上述屏幕截图在两个
richtextbox
上显示的文本相同,没有差异。因此,不应该有任何突出显示,但它显示了一些文本上的突出显示。请解释一下。谢谢。另外,我们需要为
列表chunklist1中的
Chunk
添加什么名称空间绿色部分被添加,与其他文本相比,红色部分被省略。在平衡中,这使得文本相同。我原以为这样就可以了,但现在我看到了一个副作用,就是人们不能继续使用现在相同的文本。按两次Diff按钮可删除所有高光。走开!我将很快调整我的答案..使用DiffMatchPatch;
richTextBox1.Text = File.ReadAllText(tfsVersionFilePath);
richTextBox2.Text = File.ReadAllText(dbVersionFilePath);
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;
   }
}
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 */
   }
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;
        }

    }

  }
}