Comparing words in two richtextbox to find difference?

后端 未结 2 1033
深忆病人
深忆病人 2021-01-16 00:08

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

2条回答
  •  情歌与酒
    2021-01-16 00:13

    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.

提交回复
热议问题