Richtextbox: Highlight a line only if line starts with "string"

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
In my richtextbox, how can I highlight any given line only if "Hello" is found at the very start of the line within the textbox.

To avoid text wrap issues and wrong highlighting, I guessing "Hello" has to appear after a environment.newline.

eg:
Hello highlights on this line
space + hello does not highlight on this line
This line contains hello but no highlight
 
Why not use the rtb.Lines array and String.StartsWith method? Sample:
VB.NET:
Dim startindex As Integer
For Each line As String In Me.RichTextBox1.Lines
    If line.StartsWith("2") Then
        Me.RichTextBox1.Select(startindex, line.Length)
        Me.RichTextBox1.SelectionBackColor = Color.Yellow
    End If
    startindex += line.Length + 1
Next
Me.RichTextBox1.SelectionStart = 0
 
Thanks. Adding the above code to the richtextbox's textchanged event produces unexpected results. The cursor remains at position 1 and character insertion goes in backwards. Highlighting works but "2" disappears after it is typed.

How would I use your method in the line highlighting code below ? Or is there just a better way ?

VB.NET:
    Dim searchword() As String = {"fox", "hound", "rat", "dog"}
    Dim ItemBkColor() As Color = {Color.Yellow, Color.LightYellow, Color.PaleVioletRed, 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:
Thanks. Adding the above code to the richtextbox's textchanged event produces unexpected results. The cursor remains at position 1 and character insertion goes in backwards. Highlighting works but "2" disappears after it is typed.
I have told you before that processing a static text and processing text while user is typing it is very different scenarioes that require special care. Processing all text for each char user types it not a good idea. I have also told you how you can take care of the selectionstart index before and after you change the selection in automated code. The code you posted is not suitable for finding lines that starts with certain keywords, RTB.Find method searches anywhere in text. Also bear in mind that the line-indexing functions of RTB uses the displayed "virtual" wrapped lines and not the physical lines.
 
Agreed. Right then, let's start afresh with your logic.

CONDITION: IF Line starts with "2"
then add spaces to right edge of richtextbox and terminate virtual line with newline
and highlight entire line as the user types "2"

What event do we use to fire your code above ?
 
Button.Click?
 
Alas! I cannot escape the textchanged/keydown event. It has to occur as user is typing. So to make the best of a bad situation, how best can I code this with as little flickering as possible ?
 
Back
Top