Results 1 to 6 of 6

Thread: find and select multiple words in richtextbox

  1. #1
    redgar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2008
    Posts
    13
    Reputation
    113

    find and select multiple words in richtextbox

    I have a richtextbox that I'm filling with text from a datagridview. Anywho, as the richtextbox is filling up I want to be able to highlight certain words that match my search word.

    My complication at this point is changing the backcolor to all the items in the richtextbox that match the search word. I can change a single word with

    txtSelection.SelectionStart = txtSelection.Find(txtSearch.Text)
    txtSelection.SelectionBackColor = Color.Yellow

    or simply:

    txtSelection.Find(txtSearch.Text)
    txtSelection.SelectionBackColor = Color.Yellow

    The search or selection both stop after finding and highlighting the first word. How would I extend, particularly the Find, to search through the entire richtextbox to select and highlight each matching word?

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,083
    Reputation
    2803
    Use one of the other Find methods where you can specify at which index to start the search.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    redgar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2008
    Posts
    13
    Reputation
    113
    None of those find methods seem to offer any help for my specific situation. I need to be able to search the entire textbox and not stop at the first item. I can't see how I can use a loop to select all the matching items.

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,083
    Reputation
    2803
    You have to use a loop. Find will only find once, if you give it a second thought, does the function return one or many results?? So you have to call Find many times. Each time you have to specify from where to start searching.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  5. #5
    redgar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2008
    Posts
    13
    Reputation
    113
    The find functionality clearly needs some tweaking because the way rtb.Find finds and selects text is done in a strange and illogical manner. There should obviously be a separate FindAll method applied to richtextboxes that finds and selects all instances of a search, which can be verified by the enormous amount of threads and tutorials of using Find in richtextboxes (all of which are overcomplicated).

    The solution to this problem is laughable, especially when I read for about 4 hours and could not find the simple answer I knew existed. I saw people write multiple functions and sub procedures to do this:

    Code:
    Dim textEnd As Integer = txtSelection.TextLength
                    Dim index As Integer = 0
                    Dim lastIndex As Integer = txtSelection.Text.LastIndexOf(txtSearch.Text)
    
                    While index < lastIndex
                        txtSelection.Find(txtSearch.Text, index, textEnd, RichTextBoxFinds.None)
                        txtSelection.SelectionBackColor = Color.Yellow
                        index = txtSelection.Text.IndexOf(txtSearch.Text, index) + 1
                    End While
    So there it is for the sake of others. I couldn't find one Google hit where this code appeared. I thank God for wisdom that Google and searching multiple forums could not impart.

  6. #6
    Glen Ruegsegger is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2014
    Posts
    1
    Reputation
    0

    ActiveReports Variation

    Thanks so much for posting this code. I was able to use it with the slight modifications below in an ActiveReports 7.0 report I am developing. I call this function from the details section format event on the report. The sContents variable contains the text in the richtextbox control.

    Public Sub SetRichTextProperties(ByVal rtb As GrapeCity.ActiveReports.SectionReportModel.RichTex tBox, Optional ByVal sContents As String = "")

    If sContents <> "" Then
    Try
    Dim sTextToFind As String = "DISMISSED"
    Dim textEnd As Integer = rtb.Text.Length
    Dim index As Integer = 0
    Dim lastIndex As Integer = rtb.Text.LastIndexOf(sTextToFind)
    While index < lastIndex
    index = rtb.Find(sTextToFind, index, SectionReportModel.RichTextBox.FindOptions.None)
    rtb.SelectionStart = index
    rtb.SelectionLength = 9 'matches length of sTextToFind, would need modifications to improve for any desired text
    rtb.SelectionFont = New Font("Times New Roman", 8, FontStyle.Bold)
    index = rtb.Text.IndexOf(sTextToFind, index) + 1
    End While
    Catch ex As Exception
    Exit Try
    End Try
    End If
    End Sub

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •