1

I am writing a code that highlight the duplicate words in a text. The code is working well when I add a button and the user have to press on the button to check for duplicates.

But I want to make an auto-checking code. I set my code in a subroutine that Handles RichTextBox.TextChanged. The problem is the code selects the target word and highlight it but the selection remains so when a new letter is typed, it clear what has been highlighted.

Here is my code:

Private Sub RichTextBox_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox.TextChanged
        Try

        Call duplicate_check()

    Catch ex As Exception
        MessageBox.Show("error in RichTextBox.TextChanged")
    End Try
End Sub

duplicate check function:

Private Sub duplicate_check()
        Try
            ' read line by line and get input 
            Dim LineByLineInput() As String = RichTextBox.Lines
            Dim selectionStart, selectionLength As Integer
            Dim i, j As Integer

            For lineNumber = 0 To UBound(LineByLineInput)
                selectionStart = 0
                selectionLength = 0
                'get index of first char index in the current line
                Dim count As Integer = lineNumber
                While count <> 0
                    selectionStart += RichTextBox.Lines(count - 1).Length + 1
                    count -= 1
                End While
                ' get line as string
                Dim line As String = RichTextBox.Lines(lineNumber)
                ' split line into array of strings
                Dim input() As String = line.Split(" ")
                'check for duplicates
                i = 0
                For j = i + 1 To UBound(input)

                    If input(i) = input(j) Then 'compare each 2 consecutive words if they are the same
                        selectionStart += input(i).Length + 1
                        selectionLength = input(i).Length
                        RichTextBox.SelectionStart = selectionStart
                        RichTextBox.SelectionLength = selectionLength
                        RichTextBox.SelectionBackColor = Color.Yellow

                    Else
                        selectionStart += input(i).Length + 1
                    End If
                    i += 1
                Next
            Next
        Catch ex As Exception
            MessageBox.Show("error duplicate_check()")
        End Try

    End Sub
halfer
  • 19,824
  • 17
  • 99
  • 186
Areej Qadomi
  • 143
  • 1
  • 1
  • 14

1 Answers1

1

After your duplicate_check call, have you tried to set the selection of the RichTextBox back to the old position ?

See below :

 Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
        Try
            ' Get current position
            Dim cur_pos As Integer = Me.RichTextBox.SelectionStart
            Call duplicate_check()
            ' Set back to old position
            Me.RichTextBox.SelectionStart = cur_pos
            ' Unselect what your sub duplicate_check has selected
            Me.RichTextBox1.SelectionLength = 0

        Catch ex As Exception
            MessageBox.Show("error in RichTextBox.TextChanged")
        End Try
    End Sub

If this solution is good for you, you should change your duplicate_check Sub to make this change and not in the RichTextBox1_TextChanged Sub

chateaur
  • 346
  • 1
  • 13
  • yes it makes another problem, if the user moved the cursor to specific location (to edit the text or s.th) the cursor will go again to the end of program .. Anyone pleease help me – Areej Qadomi Apr 05 '17 at 11:14