Link to home
Start Free TrialLog in
Avatar of carpbyte
carpbyte

asked on

Rich Text Box does not find text spanning lines

I can' t believe this can't be done -

I have a rich text box with multiple lines of text. If I attempt to search for a phrase that SPANS lines, the text is NOT found.

Ex.

Here's a line of
multiple text

rtf.Find("of multiple", iLastHit, RichTextBoxFinds.None) will return NO hit (I assume because between "of" and "multiple" is an OD OA.)

Even if you modify the string to INCLUDE the 0D0A it STILL doesn't find the text.

I can't believe that a rich text box doesn't allow a search across multiple lines...

Any ideas?

Thanks.





iLastHit = rtf.Find(TextBox1.Text, iLastHit, RichTextBoxFinds.None)
 
        If iLastHit = -1 Then
            iLastHit = 0
            MsgBox("Text not found", MsgBoxStyle.Information, "Search Transcript")
        Else
            rtf.SelectionStart = iLastHit
            rtf.SelectionLength = txt.Text.Length
            rtf.SelectionBackColor = Color.Red
            iLastHit = iLastHit + 1
        End If

Open in new window

Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Use regular expressions....
       Dim patt As String = TextBox1.Text.Replace(" ", "\s*[\r\n]*")
        Dim mch As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(RichTextBox1.Text, patt, _
                                                               System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        If mch.Success Then
            With RichTextBox1
                .SelectionStart = mch.Index
                .SelectionLength = TextBox1.TextLength
                .SelectionBackColor = Color.Red
            End With
        Else
            MsgBox("Text not found", MsgBoxStyle.Information, "Search Transcript")
        End If

Open in new window

Avatar of carpbyte
carpbyte

ASKER

This works, but how do I set the search start point AFTER the last find in order to find additional instances of the phrase?

(note - I did find a solution by creating a text string out of ALL of the rtf and then replacing the Chr(10), however this looks like a more elegant soultion if it can search  past each found location...)

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah I just thought of that...

Thanks