Thursday, December 19, 2013

If string was found in the RichTextBox, High Light ,Replace , Skip ..

Dim start As Integer = 0
    Dim indexOfSearchText As Integer = 0
'********************Replace Select Use Replace btn****************
        Dim startindex As Integer = 0

        If txt_Name_Changed.Text.Length > 0 Then
            startindex = FindMyText(txt_Name_Changed.Text.Trim(), start, RichTextBox1.Text.Length)
        End If
        ' If string was found in the RichTextBox, highlight it
        If startindex >= 0 Then
            ' Set the highlight color as red
            RichTextBox1.SelectionBackColor = Color.Plum
            RichTextBox1.SelectedText = lblpname.Text
            ' Find the end index. End Index = number of characters in textbox
            Dim endindex As Integer = txt_Name_Changed.Text.Length
            ' Highlight the search string
            RichTextBox1.Select(startindex, endindex)
            ' mark the start position after the position of last search string
            start = startindex + endindex
        End If

 '********************Skip ^^^ Use SKIP btn**************
        Dim startindex As Integer = 0
        If txt_Name_Changed.Text.Length > 0 Then
            startindex = FindMyText(txt_Name_Changed.Text.Trim(), start, RichTextBox1.Text.Length)
        End If
        If startindex >= 0 Then
            RichTextBox1.SelectionBackColor = Color.White
            Dim endindex As Integer = txt_Name_Changed.Text.Length
            RichTextBox1.Select(startindex, endindex)
            start = startindex + endindex
        End If

**************** Function For Replace And Skip ***********************
 Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
        ' Unselect the previously searched string
        ' ''If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
        ' ''    RichTextBox1.Undo()
        ' ''End If
        ' Set the return value to -1 by default.
        Dim retVal As Integer = -1
        ' A valid starting index should be specified.
        ' if indexOfSearchText = -1, the end of search
        If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
            ' A valid ending index
            If searchEnd > searchStart OrElse searchEnd = -1 Then
                ' Find the position of search string in RichTextBox
                indexOfSearchText = RichTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
                ' Determine whether the text was found in richTextBox1.
                If indexOfSearchText <> -1 Then
                    ' Return the index to the specified search text.
                    retVal = indexOfSearchText
                End If
            End If
        End If
        Return retVal
    End Function

**************write TextChanged  Event on Matching String Txt Box*******************
 Private Sub txt_Name_Changed_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Name_Changed.TextChanged
        start = 0
        indexOfSearchText = 0
    End Sub

^^^^^^^^^^^^^^^^^^^If string was found in the RichTextBox, Then  High Light .^^^^^^^^^^^^^^^^^^^
 Sub FindTxtInRTB() ' HighLight Search txt In RTF
        Dim srchterm As String = txt_Name_Changed.Text
        Dim index As Int32 = 0
        While index <> -1
            index = RichTextBox1.Find(srchterm, index, RichTextBoxFinds.None)
            If index <> -1 Then

                RichTextBox1.SelectionBackColor = Color.Orange
                index += 1
            End If
        End While
        RichTextBox1.DeselectAll()
    End Sub