Results 1 to 3 of 3

Thread: [RESOLVED] Find the second occurance of string rtf

Threaded View

  1. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Find the second occurance of string rtf

    Since you want to just find the 2nd occurance it is easy... What if you want to find the 25th occurance????

    I have written a small function... Try it out...

    Requirement: 1 form, 1 RTB, 1 Command

    Code:
    Option Explicit
    Dim Ret
    Private Sub Command1_Click()
        '-- usage
        '-- FindText(RichTextBox1, Search String, nth occurance)
        
        '-- Will find and color the 3rd occurance
        Ret = FindText(RichTextBox1, "Sid", 3)
    End Sub
    
    Private Function FindText(Rich As RichTextBox, sFindString As String, Numb As Integer)
        Dim FoundStrpos As Long, FoundStrLen As Long, CurInitStart As Long
        Dim CurInitLen As Long, count As Integer, lColor As Long
        
        '-- Color Green
        lColor = vbGreen
        
        '-- Save the cursor's current length and location
        CurInitStart = Rich.SelStart
        CurInitLen = Rich.SelLength
        
        '-- Length of the string to find
        FoundStrLen = Len(sFindString)
    
        '-- Find the first match
        FoundStrpos = Rich.Find(sFindString, 0, , rtfNoHighlight)
        If FoundStrpos > -1 Then
            Rich.SelStart = FoundStrpos
            Rich.SelLength = FoundStrLen
            count = count + 1
        End If
        
        While FoundStrpos > -1
            '-- Exception -- if you want to JUST find the first occurance
            If Numb = 1 Then
                '-- if found then color
                Rich.SelColor = lColor
                
                '-- Assign any number below -1 to get out of loop
                FoundStrpos = -2
            ElseIf count > 0 And count < Numb Then
                'Attempt to find the next match
                FoundStrpos = Rich.Find(sFindString, _
                FoundStrpos + FoundStrLen, , rtfNoHighlight)
                
                Rich.SelStart = FoundStrpos
                Rich.SelLength = FoundStrLen
                
                '-- if found then color
                If count = (Numb - 1) Then Rich.SelColor = lColor
                
                '-- Increment Count
                count = count + 1
            Else
                '-- Assign any number below -1 to get out of loop
                FoundStrpos = -2
            End If
        Wend
    
        '-- Restore the cursor to its original status
        Rich.SelStart = CurInitStart
        Rich.SelLength = CurInitLen
    End Function
    Last edited by Siddharth Rout; Feb 17th, 2009 at 02:28 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width