如何比较两个富文本框中的文本 [英] How to compare text in two rich text boxes

查看:79
本文介绍了如何比较两个富文本框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将富文本框中的文本与richTextBox中的文本进行比较两次,然后突出显示textBox2中的不同部分....我使用了这些代码......但它并没有高亮显示所有





Am trying to compare the text in rich textbox, with the ones in richTextBox two, then highlight the diffrent portion in textBox2....i used these codes...but its not higlighting all


richTextBox2.SelectionBackColor = Color.DarkRed;
      //  string[] Original = richTextBox1.Lines;
        //string[] Original1 = richTextBox2.Lines;

        string[] Test1 = richTextBox1.Text.Split(new char[] {' '});
        string[] Test2 = richTextBox2.Text.Split(new char[] {' '});
      //  bool? result = null;
        int k = 0;
        if(Test1.Length>Test2.Length)
        {
            k = Test1.Length;
        }
        else
        {
            k = Test2.Length;
        }
        for (int i = 0; i < k; i++ )
        {
            //Original.Equals(Original1, StringComparison.Ordinal);
            var word1 =Test1[i];
            var word2 = Test2[i];

            if (word1 != word2)
            {
                richTextBox2.SelectionStart = i;
                richTextBox2.SelectionLength = i + 1;
            }
        }
    }

推荐答案

此代码远未奏效。首先,您可以通过拆分数组中的单词索引进行选择,而不是通过单词中字符的索引进行选择。当用标点字符分隔单词时,你的分裂不能处理这些事情(这可能是标点错误但仍然发生)。差异本身并没有明确定义。当匹配单词改变位置时(例如,在它们之间插入一些附加文本),你需要处理这些情况,但仍然按相同顺序排列并匹配。我建议你研究一些众所周知的不同的操作,并为你制定一些好的要求。



最后,使用选择不是一个好主意。当用户点击某些文本时,选择将消失,点击删除按钮将删除所有选定的文本。相反,你应该用特殊的背景/前景色标记你的差异。



-SA
This code is far from working. First of all, you select by indices of words in the split array, not by indices of the characters in the words. Your split does not handle such things when words are separated by punctuation characters (which could be punctuation error but still happens). The difference itself is not well defined. You need to handle the cases when matching words changed the position (say, you inserted some additional text between them), but still placed in the same order and matching. I suggest you study the operation of some well-known differs and formulate some good requirements for yours.

And finally, using selection is not a good idea. When the user clicks on some text, the selection will disappear, and hitting delete button will delete all selected text. Rather, you should mark your differences with special background/foreground color.

—SA


我不确定,但我猜想,SelectionStart和SelectionLength应该用字符而不是单词来衡量。

哦,你可以用int k = Math.Max替换你的if(...) Test1.Length,Test2.Length)。
I am not sure but I would guess that SelectionStart and SelectionLength should be measured in chars and not in words.
Oh and you can replace your if(...) by int k = Math.Max(Test1.Length, Test2.Length).


public void check()

{



列表与LT;串GT; Test1 = new List< string>(richTextBox1.Text.Split(new char [] {''}));

List< string> Test2 = new List< string>(richTextBox2.Text.Split(new char [] {''}));



if(Test1.Count> Test2。数)

{

for(int i = 0; i< = Test1.Count - Test2.Count; i ++)

{

Test2.Add(a);

}

}

else if(Test2.Count> Test1.Count)

{

for(int i = 0; i< = Test2.Count - Test1.Count; i ++)

{

Test1.Add(a);

}

}







bool [] value = new bool [Test1.Count];

for(int i = 0; i< Test1 .Count; i ++)

{

string a = Test1 [i];

string b = Test2 [i];

val ue [i] = string.Equals(a,b,StringComparison.Ordinal);



}





for(int i = 0;我< value.Length; i ++)

{

if(value [i] == false)

{

richTextBox2.SelectionStart = (int)(richTextBox2.Text.IndexOf(Test2 [i]));

richTextBox2.SelectionLength = Test2 [i] .Length;

richTextBox2.SelectionColor = Color。红色;

}

}



Test1.Clear();

Test2.Clear();



}



它不那么专业..但它的工作除了我不知道如何确保两个richTextBox中的第一个单词不同的情况得到妥善处理
public void check()
{

List<string> Test1 = new List<string>(richTextBox1.Text.Split(new char[] { ' ' }));
List<string> Test2 = new List<string>(richTextBox2.Text.Split(new char[] { ' ' }));

if (Test1.Count > Test2.Count)
{
for (int i = 0; i <= Test1.Count - Test2.Count; i++)
{
Test2.Add("a");
}
}
else if (Test2.Count > Test1.Count)
{
for (int i = 0; i <= Test2.Count - Test1.Count; i++)
{
Test1.Add("a");
}
}



bool[] value = new bool[Test1.Count];
for (int i = 0; i < Test1.Count; i++)
{
string a = Test1[i];
string b = Test2[i];
value[i] = string.Equals(a, b, StringComparison.Ordinal);

}


for (int i = 0; i < value.Length; i++)
{
if (value[i] == false)
{
richTextBox2.SelectionStart = (int)(richTextBox2.Text.IndexOf(Test2[i]));
richTextBox2.SelectionLength = Test2[i].Length;
richTextBox2.SelectionColor = Color.Red;
}
}

Test1.Clear();
Test2.Clear();

}

Its not so professional..but its working except i dont know how to make sure cases where by the First word in both richTextBox is diffrent is properly handled


这篇关于如何比较两个富文本框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆