比较两个文档并更改文本颜色

发布于 2024-11-27 12:05:50 字数 3815 浏览 1 评论 0原文

我希望使用 RichTextBoxes 比较 2 个文件,并将它们上传到新的 RichTextBoxes 中,其中相同的文本为绿色,不同的文本为红色。

我的意思是:

文档 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............

文档 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..........................

我想匹配 第一个文件中的第一列和第二列,查看它们是否存在于第二个文件中的任何行上。如果它们匹配,则匹配的项目会将匹配的文本变为绿色,其他所有内容变为红色。

  • 有办法做到这一点吗?
  • 我如何才能将第一列与不同文件中的列进行比较?
  • 是否可以更改 RTB 中文本的颜色而不是整行的颜色?

编辑:

    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 = splitLines[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);
                }
            }
        }

但是,这似乎无法正常工作......!

最左边的所有列都应该完全绿色,但由于某种原因,有些是黑色的,有些是黑绿相间的。另外,下一列应该已经找到所有内容并将颜色更改为完整的蓝色..这就是结果看起来就像使用上面的代码。

所发生情况的新屏幕截图。

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 = splitLines[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-12-04 12:05:50

您需要创建一个流程来逐行提取所需的原始值。它看起来不像您的文件采用平面格式并且不使用分隔符,因此提取这些值可能有点棘手...您在评论中提到我们的数据是空格分隔的。在这种情况下,您可以对空格进行拆分,并使用数组的前两个元素创建搜索字符串。

一旦您有办法将这些列与文档的其余部分分开,请循环浏览并调用类似以下内容的内容:(

if (richTextBox2.Find(mystring)>0)
{
    int my1stPosition=richTextBox1.Find(strSearch);
    richTextBox2.SelectionStart=my1stPosition;
    richTextBox2.SelectionLength=strSearch.Length;
    richTextBox2.SelectionFont=fnt;
    richTextBox2.SelectionColor=Color.Green;
} 

代码主要取自 http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/651faf9b-ae32-4c99-b619-d3afd89477e1/

“SelectionColor”基本上告诉RTB 更改所选文本的颜色。您可以让程序通过“SelectionStart”和“SelectionLength”自动为您选择文本。

显然,将字体参数更改为您想要的任何内容。如果您想将文档的其余部分突出显示为红色,您可能需要考虑默认将新的 RTB 设置为红色,因为听起来好像它仅用于比较。

以上仅适用于第一次出现。如果您希望它突出显示所有出现的情况,您可能需要查看 IndexOfAll。有关详细信息,请参阅此页面:http://www.dijksterhuis.org/manipulated-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/

IndexOfAll 将返回一个数组,其中包含子字符串位于另一个字符串中的每个位置的列表。找到这些后,循环遍历数组并使用上面列出的相同代码来更改每组的颜色。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文