I am looking to compare 2 files using RichTextBoxes and upload them into new RichTextBoxes with the certain text that is the same in green and the different text in red.
What I mean is:
DOCUMENT 1
C1 147417 111.111 222.222 0 TEXT
U13 IC-123456 1234 9876 360 TEXT
R123 13866 -99.9 123.456 100 TEXT
U24 IC-123456 -14 -50 90 TEXT
............more lines............
DOCUMENT 2
1 U13 IC-123456 SOMETEXT 1.00 EA P C n Y
EC5547,3-UP 50
1 U24 IC-123456 SOMETEXT 1.00 EA P C n Y
EC5547,3-UP 50
1 C1 147417 TEXT 2.00 EA P C n Y
0603,EC0303 50
1 R123 138666 MORETEXT 2.00 EA P C n Y
50
......................more lines..........................
And I would like to match the 1st and 2nd columns in the first file to see if they exist on any line in the second file. If they match, the matched items would turn the matched text green and everything else red.
- Is there anyway to do this?
- How could I go about comparing the 1st to columns in a different file?
- Is it possible to change the color of text in a RTB and not the entire line?
EDIT:
private void checkMatchesInGCandBOM()
{
// Splits the text up to compare with the other text.
var combinedSplit = combinedPlacementsRichTextBox.Text.Split('\n');
string[] splitLines;
foreach (var line in combinedSplit)
{
Match theMatch = Regex.Match(line, @"^.*");
if (theMatch.Success)
{
// Stores the matched value in string output.
string output = theMatch.Value;
// Replaces the tabs with spaces.
output = Regex.Replace(output, @"\s+", " ");
splitLines = output.Split(' ');
int pos = 0, pos2 = 0;
pos = bomRichTextBox.Find(splitLines[0], pos, RichTextBoxFinds.MatchCase);
pos2 = bomRichTextBox.Find(splitLines[1], pos2, RichTextBoxFinds.MatchCase);
while (pos != -1)
{
if (bomRichTextBox.SelectedText == splitLines[0] && bomRichTextBox.SelectedText != "")
{
int my1stPosition = bomRichTextBox.Find(splitLines[1]);
bomRichTextBox.SelectionStart = my1stPosition;
bomRichTextBox.SelectionLength = splitLines[0].Length;
bomRichTextBox.SelectionFont = new System.Drawing.Font("Arial", 8, FontStyle.Underline);
bomRichTextBox.SelectionColor = Color.Green;
}
pos = bomRichTextBox.Find(splitLines[0], pos + 1, RichTextBoxFinds.MatchCase);
}
while (pos2 != -1)
{
if (bomRichTextBox.SelectedText == splitLines[1] && bomRichTextBox.SelectedText != "")
{
int my1stPosition = bomRichTextBox.Find(splitLines[0]);
bomRichTextBox.SelectionStart = my1stPosition;
bomRichTextBox.SelectionLength = splitLin开发者_开发技巧es[1].Length;
bomRichTextBox.SelectionFont = new System.Drawing.Font("Arial", 8, FontStyle.Underline);
bomRichTextBox.SelectionColor = Color.Blue;
}
pos2 = bomRichTextBox.Find(splitLines[1], pos2 + 1, RichTextBoxFinds.MatchCase);
}
}
}
However, this does not seem to be working properly....!
All of the columns on the far left should have been COMPLETELY green but for some reason some are black and some are black and green. Also the next column should have been found everything and changed the color to complete blue..This is what it turned out to look like using the code above.
New screenshot of what happens.
You'll need to create a process that pulls the original values that you want line-by-line. It doesn't look like your file is in a flat format and doesn't use delimiters, so pulling those values might be a bit tricky... You mentioned in the comments that our data is space delimited. In this case you can do a split on spaces and create your search string with the first two elements of the array.
Once you have a way to separate those columns from the rest of your document, cycle through and call something something like this:
if (richTextBox2.Find(mystring)>0)
{
int my1stPosition=richTextBox1.Find(strSearch);
richTextBox2.SelectionStart=my1stPosition;
richTextBox2.SelectionLength=strSearch.Length;
richTextBox2.SelectionFont=fnt;
richTextBox2.SelectionColor=Color.Green;
}
(code mostly taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/651faf9b-ae32-4c99-b619-d3afd89477e1/)
The "SelectionColor" basically tells the RTB to change the color of the selected text. You are making the program automatically select the text for you with "SelectionStart" and "SelectionLength".
Obviously change the font params to whatever you'd like. If you want to highlight the rest of the document red, you might want to consider making the new RTB red by default, since it sounds as if it's only used for the comparison.
The above will only work for the first occurrence. If you want it to highlight ALL occurrences, you might want to check out IndexOfAll. See this page for more info: http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/
IndexOfAll will return an array with a list of each position a substring lies in another string. Once you find these, loop through the array and use the same code that's listed above to change the color of each set.
精彩评论