I have three RichTextBoxes
. I want to compare all the words of RichTextbox1
with Richtextbox2
one by one with space or comma as the de
can someone please help me with coloring the text ?? – Vineet Kamath Mar 1 at 17:30
If you want to color or highlight your text you just need (1) to find and select the word/string and (2) to set text properties.
Try to modify Error404's code in this way:
Dim diffPosition as integer ' Set where beging 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 RichTextBox1
.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
diffPosition = 1 ' re-Initialize for RichTextBox2
For Each diff As String In txt2
If Array.IndexOf(txt1, diff.ToString) = -1 Then
diff2 += diff.ToString & " "
With RichTextBox2
.Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox2 starting from position diffPosition in RichtextBox2
.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
RichTextbox3.Text = diff1 & diff2
The code shall find and select "diff", set Bold style, set color of each letter in blue (instead of black) and highlight in yellow.
First we will declare some variables to shorten our writing work. We'll then use the For Each command.
Here we will take two rounds to scan the differences, first of Richtextbox1
which is not in Richtextbox2
and vice versa. The different characters will keep adding to the variable diff1
and diff 2
and at the end we will compile both of them in RichTextbox3
.
It will work on the diff
algorithm.
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 = "" 'Differences between 1 and 2
Dim diff2 As String = "" 'Differences between 2 and 1
For Each diff As String In txt1
If Array.IndexOf(txt2, diff.ToString) = -1 Then
diff1 += diff.ToString & " "
End If
Next
For Each diff As String In txt2
If Array.IndexOf(txt1, diff.ToString) = -1 Then
diff2 += diff.ToString & " "
End If
Next
RichTextbox3.Text = diff1 & diff2
End Sub
Hope it would work perfectly!