Compare two textboxes and highlight the differences

mahie

Registered User.
Local time
Today, 07:24
Joined
May 16, 2013
Messages
12
Hi,
I have a code to compare contents of two text boxes and highlight the differences which is working fine but due to this even if one character has changed the entire text after the change is highlighted in red color.

what I want to do is to change this code to compare text line by line to highlight the differences.. Can you please help me with this..
The code that I already have is as below:

For i = 1 To Len(InkNew.Value)
InkNew.SelStart = i - 1
InkNew.SelLength = 1
If Mid(InkNew.Value, i, 1) = Mid(InkOld.Value, i, 1) Then
InkNew.SelStart = i
InkNew.SelLength = 1
InkNew.SelColor = vbBlack
Else
InkNew.SelStart = i - 2
InkNew.SelLength = 1
InkNew.SelColor = vbRed
End If
Next
x = x + 1
 
Hi,
I am not using a normal text box instead I have used "Microsoft InkEdit Control" to do the highlighting part.. The highlight is working fine but its just that instead of the whole text box i want to compare the two text boxes line by line.. and highlight the differences in each line..
It is possible that only one word in 1 out of 3 lines has changed but with this current code after the change the remaining text by default gets highlighted in red which is not what i want.

Regards,
Mahie
 
Code:
If Mid(InkNew.Value, i, 1) = Mid(InkOld.Value, i, 1) Then

Note: Access / VBA do cAsElEsS comparisons when using the '=' operator.

To get CaSeSeNsItIvE comparisons, I suggest you use the StrComp function with the vbBinaryCompare option selected. Example reworked LOC:

Code:
If StrComp(Mid(InkNew.Value, i, 1), Mid(InkOld.Value, i, 1), vbBinaryCompare) <> 0 Then
 
Hi Michael,
Thank you for your inputs.. I replaced the line in the code as per your suggestion..

however the output still remains the same..

e.g: Textbox 1: This is first line.
This is second line..
Textbox 2: This. is first line.
This is second line..

then if I am comparing these two contents then due to the "." in textbox2 the remaining words after "." get highlighted in red but I dont want the 2nd line to be highlighted in red as the contents of 2nd line in textbox 1 and textbox 2 is the same...
I know this is a complex thing but I have to get a way out of this .. :(
 
As you are comparing the same character position in each textbox, once you have an extra character in one box you are now out of synch and no more characters will match, hence t hey will be turned red.

Brian
 
you are right Brian and thats the precise reason I want to modify my code to compare line by line.. so that atleast the next line will not turn red but am not able to find a logic codewise to achieve this..
 
So is this data all in one string, with vbCrLf characters embedded?
 
Yes absolutely right.. and I want to compare the string line by line by extracting it using instr.. but I am not able to get the logic ahead of it for highlighting..
 
All right, so you will need to have code to compare a virtual line by line, so a loop breaking the entire string at vbCrLf's, and if a difference is spotted within the virtual lines, then mark that one line red.
 
Not the entire line.. but maybe the word or the character as red.. as this will give my user more precision on what has changed between the old and the new values
 
As there are infact no lines only what you have described as virtual lines existing between vbCrlf's then he is still working in character position mode, he could on error set the error position red and then find the next vbCrlf and start again. He would need to keep separate position in each textbox iold and inew maybe and his loop would be Do until inew > len(inknew) maybe.

Brian

Mahie posted whilst I was thinking and typing
 
Hi,
I have now modified my code to compare each word.. however while highlighting it does some vague behaviour.. it does not highlight some parts of the text or highlights is wrongly.. can you tell me what am i missing in this code.

Private Sub CompareText1()
Dim txtold() As String, txtnew() As String
txtold = Split(InkOld.Value, " ")
txtnew = Split(InkNew.Value, " ")
ss = 0
ss1 = 0
If UBound(txtnew) > UBound(txtold) Then
ubb = UBound(txtold)
Else
ubb = UBound(txtnew)
End If
st = 1
For i = 0 To ubb
If StrComp(txtnew(i), txtold(i), vbTextCompare) = 0 Then
st = InStr(st, InkNew.Value, txtnew(i))
lenn = Len(txtnew(i))
InkNew.SelStart = st - 1
InkNew.SelLength = lenn + 1
InkNew.SelColor = vbGreen
Else
st = InStr(st, InkNew.Value, txtnew(i))
lenn = Len(txtnew(i))
InkNew.SelStart = st - 1
InkNew.SelLength = lenn + 2
InkNew.SelColor = vbRed
End If
Next
End Sub
 
Without seeing the errors w e can only guess, but to me you have still not addressed the issue of synchronicity , I suggested using the new line characters, thus you would be able to indicate which lines had a difference, I can't believe that you need to show each actual difference.

Spaces do not necessarily work consider

"Which is what he said, so I etc"
And
" which is what he said , so I etc"

Brian
 
Hi Brian,
I need to highlight the details exactly as it will save on my users time in identifying the exact changes that the customer has sent..
The details may run into a multiple number of lines thus making it difficult for my user to understand what exactly has changed..
Now I have modified my code more so that I am opening an excel sheet for the user which is showing me almost exact results.. which I am not able to achieve using text box..
the code is almost the same that I had listed earlier..

:)
 

Users who are viewing this thread

Back
Top Bottom