gpt4 book ai didi

c# - 如何比较两个富文本框内容并高亮显示变化的字符?

转载 作者:太空狗 更新时间:2023-10-30 00:02:53 31 4
gpt4 key购买 nike

我用来读取2个richtextbox内容的代码如下:

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

现在,我需要比较两个富文本框的内容并突出显示两个富文本框中更改的字符。目的是通过 C# 应用程序获取差异并突出显示字符 ,如 TFS(比较文件)。谢谢。

编辑:

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

我从调试中了解到,指向文本光标的 richTextBox1 的 SelectSelectionColorSelectionBackColor 方法增加到 7执行特定行后的位置。如何保持richTextBox1的光标位置

最佳答案

首先感谢 ArtyomZzz 指出了 DiffMatchPatch 的重要来源!

这是一段代码,它会在单击按钮时在两个 RichTextbox 中绘制更改后的字符。

首先下载diff-match-patchsource . (!请参阅下面的更新!)从 zip 文件复制“DiffMatchPatch.cs”和“COPY”到您的项目,并将 cs 文件包含在您的项目中。还将 namespace 添加到您的 using 子句中。

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

}

}
}

一开始我也试着把变化的线条作为一个整体涂成较浅的颜色;然而,这需要更多的工作,无法完成(给整行着色,而不是仅仅为内容部分着色),并且一开始就不是你问题的一部分..

注意:有四种不同的差异后清理方法。哪个最合适取决于输入和目的。我建议尝试 cleanupSemanticLossless。我添加了第三个屏幕截图,显示了此清理的工作原理

这是输出的屏幕截图: original output

还有一个新版本: new screenshot

清理后的截图SemanticLossless: 3rd screenshot

更新:源似乎已经移动。 This应该有帮助..

关于c# - 如何比较两个富文本框内容并高亮显示变化的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887238/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com