user13770900
user13770900

Reputation:

Comparing text in two Richtextboxes and get the differences

I'm want to compare the text between two richtextboxes and get the differences in the third one. Without highlight the text. So far, the best option is this solution

The first solution works, but it doesn't remove the text present in richtextbox2 from the richtextbox1. Endeed, the user asked

if they are the same do nothing.

My case is complete the opposite and still i cannot find a solution.

Thanks

Upvotes: 1

Views: 293

Answers (1)

GME
GME

Reputation: 156

First, you need to add a combobox to your form named (combobox1) then add these items in it:

RichTextbox1 - RichTextbox2

RichTextbox2 - RichTextbox1

second, add a button named (button1), under this button click event insert this code:

RichTextBox3.Clear()
If RichTextBox1.Text <> "" And RichTextBox2.Text <> "" And RichTextBox1.Text <> RichTextBox2.Text And ComboBox1.SelectedItem = "RichTextbox1 - RichTextbox2" Then
    Dim txt1(RichTextBox1.Text.Split(" ").Length) As String
    Dim txt2(RichTextBox2.Text.Split(" ").Length) As String
    txt1 = RichTextBox1.Text.Split(" ")
    txt2 = RichTextBox2.Text.Split(" ")
    Dim diff1 As String = ""
    For Each diff As String In txt1
        If Array.IndexOf(txt2, diff.ToString) = -1 Then
            diff1 += diff.ToString & " "
        End If
    Next
    RichTextBox3.Text = diff1.ToString
End If

If RichTextBox1.Text <> "" And RichTextBox2.Text <> "" And RichTextBox1.Text <> RichTextBox2.Text And ComboBox1.SelectedItem = "RichTextbox2 - RichTextbox1" Then
    Dim txt1(RichTextBox1.Text.Split(" ").Length) As String
    Dim txt2(RichTextBox2.Text.Split(" ").Length) As String
    txt1 = RichTextBox1.Text.Split(" ")
    txt2 = RichTextBox2.Text.Split(" ")
    Dim diff2 As String = ""
    For Each diff As String In txt2
        If Array.IndexOf(txt1, diff.ToString) = -1 Then
            diff2 += diff.ToString & " "
        End If
    Next
    RichTextBox3.Text = diff2.ToString
End If

now, you have 2 options: if you choose (RichTextbox1 - RichTextbox2) from the combobox then click the button, richtextbox3 will display the text which is found in richtextbox1 and not found in richtextbox2, while if you choose (RichTextbox2 - RichTextbox1), the opposite will happen

finally, if the 2 richtextboxes is the same, nothing will occur

  • Also you could use String.Join *

Under Button1 click event replace this code with the previous one:

Dim intsA = RichTextBox1.Text.Split(" ")
Dim intsB = RichTextBox2.Text.Split(" ")
Dim myresult = intsA.Except(intsB).ToArray()
RichTextBox3.Text = String.Join(" ", myresult)

if you found this useful, mark it as answer

Upvotes: 1

Related Questions