Sunday, June 17, 2012

Pencarian Secara Recursive Pada RichTextBox

Private Sub Form_Load()

RichTextBox1.LoadFile "license.txt"

End Sub

Private Sub Command1_Click()

Dim strval As String
Dim nStrings As Long

RichTextBox1.LoadFile "license.txt"

strval = " " & InputBox("Enter the string to find.", "Findit", "the") & " "

If strval <> "" Then

nStrings = FindIt(RichTextBox1, strval)
MsgBox (Str$(nStrings) & " instances found.")
End If

End Sub

Private Function FindIt(Box As RichTextBox, Srch As String, Optional Start As Long)

Dim retval As Long
Dim Source As String

Source = Box.Text

If Start = 0 Then Start = 1

retval = InStr(Start, Source, Srch)

If retval <> 0 Then

With Box
.SelStart = retval - 1
.SelLength = Len(Srch)
.SelColor = vbRed
.SelBold = True
.SelLength = 0
End With

Start = retval + Len(Srch)

FindIt = 1 + FindIt(Box, Srch, Start)
End If
End Function