WMK
WMK

Reputation: 33

Showing the difference between two RichTextBox controls

I'm trying to compare both richtextbox text and show the difference into the 3rd richtextbox. After i do some changes to the code that i get from this forum, it still have some problems, which is there are words that are no different showing out at my 3rd richtextbox.... the right hand side of the rich text box is from a text file that have been checked in regex function before displayed in the box.

this is the source code that use for compare:

Dim txt1(DispBox.Text.Split(" ").Length) As String
Dim txt2(DispBox2.Text.Split(" ").Length) As String
txt1 = DispBox.Text.Split(" ")
txt2 = DispBox2.Text.Split(" ")
Dim diff1 As String = "" 'Differences between 1 and 2
Dim diff2 As String = "" 'Differences between 2 and 1
Dim diffPosition As Integer ' Set where begin to find and select in RichTextBox

diffPosition = 1 ' Initialize
For Each diff As String In txt1
  If Array.IndexOf(txt2, diff.ToString) = -1 Then
    diff1 += diff.ToString & "  "
    With DispBox
      .Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox1 starting from position diffPosition in RichtextBox1
      .SelectionFont = New Font(.Font, FontStyle.Bold) ' Set diff in Bold
      .SelectionColor = Color.Blue ' Set diff in blue instead of black
      .SelectionBackColor = Color.Yellow ' highlight in yellow
    End With
  End If
  diffPosition = diffPosition + Len(diff) ' re-Initialize diffPostion to avoid to find and select the same text present more than once
Next

DispBox3.Visible = True
DispBox3.Text = diff1

problems this is my upload button code to check the regex function

Dim result As DialogResult = OpenFileDialog1.ShowDialog()

' Test result.
If result = Windows.Forms.DialogResult.OK Then

  ' Get the file name.
  Dim path As String = OpenFileDialog1.FileName
  Try
    ' Read in text.
    Dim text As String = File.ReadAllText(path)
    Dim postupload As String = Regex.Replace(text, "!", "")
    DispBox2.Text = postupload

    ' For debugging.
    Me.Text = text.Length.ToString
  Catch ex As Exception
    ' Report an error.
    Me.Text = "Error"
  End Try
End If

because inside the text file there will be "!" between the line, I would like to replace the "!" with "breakline/enter".

My problem is :

  1. why the "Building & hostname" words count as wrong words.
  2. why the new word that display in 3rd richtextbox is not in new line if the words is found in the middle of the line.
  3. the other wrong words are not color, bold n highlight.....

Upvotes: 3

Views: 1051

Answers (1)

LarsTech
LarsTech

Reputation: 81620

Your code is splitting all the words based on a space, but it's ignoring the line breaks, so it makes "running-confing building" look like one word.

Try it this way:

Dim txt1 As String() = String.Join(" ", DispBox.Lines).Split(" ")
Dim txt2 As String() = String.Join(" ", DispBox2.Lines).Split(" ")

Upvotes: 1

Related Questions