Richtextbox : highlight entire line based on found word

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
In a richtextbox, how can I highlight the entire line where a given word is found. eg "fox"

Thanks for any help.
 
VB.NET:
Dim index As Integer = Me.RichTextBox1.Find("fox")
If index <> -1 Then
    Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
    Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
    Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
    If last = -1 Then last = Me.RichTextBox1.TextLength
    Me.RichTextBox1.Select(first, last - first)
    Me.RichTextBox1.SelectionBackColor = Color.Yellow
End If
 
The above code correctly highlights the line containing "fox"

How can I extend the highlighting from the beginning of the line to the right edge of the richtextbox ? ie: the full line within the richtextbox.

Also how can I recursively find and highlight all lines containing fox ?
 
You can only treat the text, if there is not text for the rest of the line I'm not aware of a way to do that.

To recurse all lines you can call Find method many times giving the index to start searching. There are several Find methods with different paramters. For example:
VB.NET:
Dim index As Integer = 0
While index <> -1
    index = Me.RichTextBox1.Find("fox", index, RichTextBoxFinds.None)
    If index <> -1 Then
        'all matches gets here
        index += 1
    End If
End While
 
Last edited:
Highlight key lines in richtextbox

I've been seeking a way to emulate a control where entire keylines in a richtextbox are highlighted.

I've attached a sample to give you an idea.

If user types in "Verse" the whole line gets highlighted...

Appreciate any help.
 

Attachments

  • verse highlight.jpg
    verse highlight.jpg
    103.5 KB · Views: 85
You can only mark the text though, filling to width of control could be done with a Listbox where is each item is a text line.
 
Thank you for the useful sample.

As the user is typing...
and if they paste into the richtextbox...

Placing the code in textchanged event, The highlight comes on after they type the space after "fox_".

BUT... it does not allow further text entry after that. Please tell me how users can continue typing without holdup after typing in "fox "

VB.NET:
    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim index As Integer = Me.RichTextBox1.Find("fox ")
        If index <> -1 Then
            Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
            Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
            Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
            If last = -1 Then last = Me.RichTextBox1.TextLength
            Me.RichTextBox1.Select(first, last - first)
            Me.RichTextBox1.SelectionBackColor = Color.Yellow
        End If
    End Sub
 
Last edited:
BUT... it does not allow further text entry after that
Yes, it does, but remember that you made a selection (RichTextBox1.Select). What happens if you select text in any editor and write some? Yes, you overwrite the selection. How can you unselect? You can't, but you can make a new selection. Where should this selection be? Where were you before you made the first selection? Yes, you were at SelectionStart. Pseudo:
VB.NET:
Dim WhereWasI As Integer = Me.RichTextBox1.SelectionStart
find text
If found
  manipulate
  Me.RichTextBox1.Select(WhereWasI, 0)
End if
 
This code will highlight every line where fox appears. Is there any way on RichTextBox1_TextChanged to have a number or array of searchwords, say
fox, hound, squirrel, etc

When fox is found, highlight the line in yellow
When hound is found,highlight the line in blue
When squirrel is found,highlight the line in green
VB.NET:
Public Class Form1

Dim searchword As String = "fox"

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim SelStart As Integer = RichTextBox1.SelectionStart

Dim count As Integer = 0

Dim srchMode As RichTextBoxFinds

srchMode = RichTextBoxFinds.WholeWord

Dim wordAt As Integer = 0

Do While wordAt <> -1

wordAt = RichTextBox1.Find(searchword, wordAt, srchMode)

If wordAt = -1 Then

Exit Do

Else

HightlightLine(wordAt)

'RichTextBox1.Select(wordAt, searchword.Length)

'RichTextBox1.SelectionBackColor = Color.Yellow

wordAt += 1

count += 1

End If

Loop

RichTextBox1.SelectionStart = SelStart

RichTextBox1.SelectionLength = 0

RichTextBox1.SelectionBackColor = Color.White

End Sub

 

Private Sub HightlightLine(ByVal wordat As Integer)

Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(wordat)

Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)

Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)

If last = -1 Then last = Me.RichTextBox1.TextLength

Me.RichTextBox1.Select(first, last - first)

Me.RichTextBox1.SelectionBackColor = Color.Yellow

 

End Sub

End Class
 
Processing text as user is typing is a quite different scenario compared to processing all current text at the click of a button. Searching the whole text over and over again for each character typed is less than ideal. You only have to process the changed text including back-forward to next "word" boundary as you define it. Take some time and try to analyze what portion of text needs to be processed and for what keypresses you need to act. For example you can define that when user presses Space or Enter you have end-index from previous char index and search back to previous space or linefeed to find start-index, this give the substring of last "word" written. Depending on circumstances it might be easier/faster to just grab the whole current line and check this for presence of some words. Insert/Paste/Delete and behaviour when editing selections are interesting topics here.
Is there any way on RichTextBox1_TextChanged to have a number or array of searchwords
Defining and filling an array/collection/dictionary of strings is pretty elementary.
 
Agreed. Text needs to be processed word by word, or line by line - not each character typed.

But all we have is the textchanged event, right ? Linefeed is the right trigger for sure.

So I need to massage the textchanged event to fire when a linefeed is found.

Defining and filling an array/collection/dictionary of strings is pretty elementary.

Blimey, how do I do it in the case of the code I provided above??
 
Last edited:
But all we have is the textchanged event, right ?
You also have all the Key... events for example.
 
Gotcha.

On key down event test for enter key.

VB.NET:
If e.KeyValue = Keys.Enter Then
'highlight the line
Else
RichTextBox1.SelectionBackColor = Color.White
eND IF

The code below works for textchanged event (with flickering obviously) but when I place it in a keydown event - it does not highlight to the end of line

VB.NET:
Public Class Form1

    Dim searchword() As String = {"fox", "dog", "cat"}
    Dim ItemBkColor() As Color = {Color.Yellow, Color.LightSteelBlue, Color.LightSeaGreen}

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

        Dim SelStart As Integer = RichTextBox1.SelectionStart
        Dim count As Integer = 0
        Dim srchMode As RichTextBoxFinds

        srchMode = RichTextBoxFinds.WholeWord

        For SearchWordPointer As Integer = 0 To searchword.Length - 1
            Dim wordAt As Integer = 0
            Do While wordAt <> -1
                wordAt = RichTextBox1.Find(searchword(SearchWordPointer), wordAt, srchMode)
                If wordAt = -1 Then
                    Exit Do
                Else
                    HightlightLine(wordAt, ItemBkColor(SearchWordPointer))
                    wordAt += 1
                    count += 1
                End If
            Loop
        Next

        RichTextBox1.SelectionStart = SelStart
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionBackColor = Color.White
    End Sub

    Private Sub HightlightLine(ByVal wordat As Integer, ByVal BG_Color As Color)
        Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(wordat)
        Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
        Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
        If last = -1 Then last = Me.RichTextBox1.TextLength
        Me.RichTextBox1.Select(first, last - first)
        Me.RichTextBox1.SelectionBackColor = BG_Color
    End Sub
 
Last edited:
Back
Top