VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Search and change color for multiple sub-strings in RichTextBox

by Marko Malinen (1 Submission)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 20th August 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Search and change color for multiple sub-strings in RichTextBox

Rate Search and change color for multiple sub-strings in RichTextBox



    Dim lWhere, lPos As Long
    Dim sTmp, sSearch As String
    
    'set lPos to 1 for mid()
    lPos = 1
    'this is the searched string
    sSearch = "DATA"
        
    'loop the whole text
    Do While lPos < Len(Me.RichTextBox1.Text)
        
        'get sub string from the text
        'this is because the InStr() returns the
        'position of first occurence of the string...
        sTmp = Mid(Me.RichTextBox1.Text, lPos, Len(Me.RichTextBox1.Text))
        
        'find the string in sub string
        lWhere = InStr(sTmp, sSearch)
        'accumulate the lPos to be relative to the actual text
        lPos = lPos + lWhere
        
        If lWhere Then   ' If found,
            Me.RichTextBox1.SelStart = lPos - 2   ' set selection start and
            Me.RichTextBox1.SelLength = Len(sSearch)   ' set selection length.   Else
            Me.RichTextBox1.SelColor = RGB(255, 0, 0) 'change color to red
        Else
            Exit Do 'we are ready
        End If
    Loop

Download this snippet    Add to My Saved Code

Search and change color for multiple sub-strings in RichTextBox Comments

No comments have been posted about Search and change color for multiple sub-strings in RichTextBox. Why not be the first to post a comment about Search and change color for multiple sub-strings in RichTextBox.

Post your comment

Subject:
Message:
0/1000 characters