I am working on a c# add-in project and i want to compare two richtextboxes and highlight the differences.
but my code will highlight the whole content of the richtextbox. But I want to highlight only the differences in the richtextbox.
this is my code,
public void Compare_HighlightContent()
{
txtSourceBefore.Text = SharedObjects.CodeBefore;
RichTextBox sourceBefore = txtSourceBefore;
txtSourceAfter.Text = SharedObjects.CodeAfter;
RichTextBox sourceAfter = txtSourceAfter;
//int counter=0;
string[] targetLinesAfter = sourceAfter.Lines;
string[] destLineBefore = sourceBefore.Lines;
List<string> result = targetLinesAfter.Except(destLineBefore).ToList<string>();
foreach (string str in result)
{
//get the line Index of 'str' value
int lineIndex = Array.IndexOf(targetLinesAfter, str);
int lineCount = 0;
int index;
for (index = 0; index < lineIndex; index++)
{
lineCount = lineCount + targetLinesAfter[index].Length;
}
sourceAfter.SelectionStart = lineCount;
sourceAfter.SelectionLength = str.Length;
// sourceAfter.SelectionColor = Color.Black;
sourceAfter.SelectionBackColor = Color.Yellow;
}
}
so please be kind enough to help me to solve this problem.