Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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


C#
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;
            }
        }
    }
Posted

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
 
Share this answer
 
Comments
[no name] 26-Aug-13 6:35am    
my vote 4 to this solution.
Sergey Alexandrovich Kryukov 26-Aug-13 13:49pm    
Thank you, Rakesh.
—SA
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).
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900