- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我用来读取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 的 Select
或 SelectionColor
或 SelectionBackColor
方法增加到 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
。我添加了第三个屏幕截图,显示了此清理的工作原理
这是输出的屏幕截图:
还有一个新版本:
清理后的截图SemanticLossless:
更新:源似乎已经移动。 This应该有帮助..
关于c# - 如何比较两个富文本框内容并高亮显示变化的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887238/
我的意思是这三个富人的论点: #{rich:clientId('id')} #{rich:element('id')} #{rich:component('id')} 例如在这些方法调用(action
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What are the differences between pointer variable and
对于实习,我将不得不开发一个桌面应用程序。重点是创建丰富的 UI(酷炫的效果、声音等)。我应该使用哪种技术? - 闪光 ? (在这种情况下,我应该使用 flex 项目吗?AIR ?这和一个简单的 ra
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我想知道这一行的作用: foo || (foo = this.foo) 在下面的函数定义中 someFunction: function(foo) { foo || (foo = thi
我有一个数据模型,其中有一个 Item可以属于一个或多个 Categories .我正在尝试显示 允许用户选择哪个Categories Item应该属于。我正在使用 RichFaces 和 Seam
我意识到我大部分时间都在使用三元运算符,如下所示: foo ? foo:酒吧; 这变得很麻烦,因为变量长度变得很长,例如。克。 appModel.settings.notifications ? ap
我要做出一个设计决定。 在我的 web(ajax) 应用程序中,我们需要决定将用户界面逻辑放在哪里? 是否应该通过 javascript 完全加载(纯单页)。只有数据来来去去。 或 服务器是否应该发送
有人向我指出我在某些 C++ 代码中有一个看起来像拼写错误的地方: protected: Foo x, y,; 我原以为结尾的逗号会出错,但显然不是?这是未定义的,还是发生了什么?大概是坏事
谁能解释一下这是做什么的? var foo = foo || alert(foo); 最佳答案 如果 foo 已经被定义并且计算结果为真,它设置 foo = foo,即它什么都不做。 如果定义了 fo
看起来这应该是可能的,但是......? 使用 richfaces 和 JSF,我正在使用 rich:dataList 遍历 List ...一切都很好,除了我希望能够选择性地“呈现”每次迭代,这可能
我有以下带有 rich:calendar 的 xhtml 文件,我正试图禁用一些日子 using this example .但是永远不会调用 javascript 函数。我不知道为什么。
我的应用程序在 90% 的时间里都使用 Ajax 调用来获取后端数据。我需要为所有这些调用实现 CSRF 预防。所以我需要在每次调用时传递 token 。我在哪里生成 token ?在客户端还是服务器
我正在尝试重构和引入一些旧代码,我遇到了这样的事情: struct foo; typedef struct foo * foo; 尝试编译它时,出现以下错误: Sourc
我想使用类似 TextView 的东西进行类似串行/TTY/文本终端的同步用户交互。我的意思是我需要以下操作: 在末尾添加一些新文本(使用各种 Span 设置样式) 同步等待用户在最后输入一些进一步的
片段 1: if ( x ) { foo(); } 片段 2: x ? foo() : 0; 这两个片段之间有什么区别? 编辑:更正了语法错误。 更新:顺便说一句,似乎还有一个更短的符号:
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 7 年前。 我的类实现了一个非常简单的 RPN 计算器原型(prototype)。
我正在我的元素中使用,我需要为其自定义 CSS。我想在文件上传按钮中删除“+”标记(参见图片),我该怎么做? 提前致谢。 最佳答案 只是覆盖样式,按钮类是rf-fu-btn-cnt-add,加号是背景
我在 jsf .xhtml 页面中有一个 richdatatable。我有一个特定单元格的很长的文本。我如何设置列的宽度并在指定的单元格中显示文本。我是 JSF 和 CSS 的新手。我正在提供一个例子
我是一名优秀的程序员,十分优秀!