|
-
Dec 17th, 2002, 10:25 PM
#1
Thread Starter
Hyperactive Member
Search And Find
Hello, fellow coders,
I've gotten some powerful code from here, yet I've got Soooo much to learn. Can anyone tell me how to make the following code stop at each find, instead of shooting through the whole search without stopping? This code simply totals up the amount of times the search text was found and turns them all red. I expect it to be simple, but simple is hard when you don't know what it is. Thanks in advance.
Option Explicit
Option Compare Text
Private Sub Command2_Click()
unload me
End Sub
Private Sub Form_Load()
RichTextBox1.LoadFile "MyFile.wav"
End Sub
Private Sub Command1_Click()
Dim strval As String 'Inputbox returns a string
Dim nStrings As Long
RichTextBox1.LoadFile "MyFile.wav"
strval = " " & InputBox("Enter the string to find.", "Findit", _
"Love") & " "
If strval <> "" Then
nStrings = FindIt(RichTextBox1, strval)
MsgBox (Str$(nStrings + 1) & " instances of """ & strval & """ found.")
End If
End Sub
Private Function FindIt(Box As RichTextBox, Srch As String, _
Optional Start As Long)
Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr
Source = Box.Text 'put the text to search into the variable
If Start = 0 Then Start = 1 'the initial call doesn't pass a value
'for Start, so it will equal 0
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
Last edited by GARY MICHAEL; Dec 18th, 2002 at 02:40 AM.
Thanks,
GARY
-
Dec 18th, 2002, 12:27 AM
#2
This isn't an answer to your question but I just wanted to point out that this code will cause you problems.
VB Code:
Private Sub Command2_Click()
End
End Sub
Instead of using End you should just unload all your forms.
-
Dec 18th, 2002, 02:43 AM
#3
Thread Starter
Hyperactive Member
Thanks for your advice, Martinliss. I've changed the code from "End" to "Unload Me".
-
Dec 18th, 2002, 07:09 AM
#4
Frenzied Member
It is at this point:
VB Code:
If retval <> 0 Then
With Box
'etc
End With
Start = retval + Len(Srch)
'Now we re-call the function to find the next occurrence of the string
FindIt = 1 + FindIt(Box, Srch, Start)
End If
So, just befre you call "FindIt" again, you can do things to pause the program (Msgbox "Click to Continue")
-
Dec 19th, 2002, 09:35 AM
#5
Thread Starter
Hyperactive Member
Thanks, JordanChris
Your code worked well. How can I replace the msgbox with a next button so that it would work like a regular search routine and stop at each highlighted find? Thanks in advanced to anybody who would respond.
-
Dec 19th, 2002, 09:45 AM
#6
Instead of using the Find method of the richtextbox, i use while loops to search the text and colorize it/count it. Tis how i made my error report for my programming language. Here's how i'd do that:
VB Code:
Option Explicit
Dim Position As Integer
Dim found As Integer
Private Sub Command2_Click()
unload me
End Sub
Private Sub Form_Load()
RichTextBox1.LoadFile "MyFile.wav"
Position = 0
found = 0
End Sub
Private Sub Command1_Click()
Dim strval As String 'Inputbox returns a string
Dim nStrings As Long
RichTextBox1.LoadFile "MyFile.wav"
strval = " " & InputBox("Enter the string to find.", "Findit", _
"Love") & " "
If strval <> "" Then
FindIt RichTextBox1, strval
MsgBox (found & " instances of """ & strval & """ found.")
End If
End Sub
Private Function FindIt(Box As RichTextBox, Srch As String)
Position = InStr(Position + 1, Box.Text, Srch, vbTextCompare)
While Position > 0
Box.SelStart = Position - 1
Box.SelLength = Len(Srch)
Box.SelColor = RGB(255, 0, 0)
found = found + 1
Position = Position + Len(Srch)
Position = InSTr(Position + 1, Box.Text, Srch)
Wend
End Function
Hope that helps
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 19th, 2002, 09:45 AM
#7
Frenzied Member
Throw these lines at the end of the function away:
VB Code:
'Now we re-call the function to find the next occurrence of the string
FindIt = 1 + FindIt(Box, Srch, Start)
Then you are all set....
But there is a little extra programming to do first!
You need the variable Start to be globally available (DIM it at the top of your program, or in a code module), so that you can call FindIt with the Start parameter each time the user clicks on "Find Next".
Make sure you know what the user wants when clicking on "Find". Do they mean: start where I have got to and look further. Or do they mean: start at the beginning and look from the beginning.
You may well need a Find and a Find Next button. The FindNext button should [probably] only be visible after the Find button has been clicked. Should it also be available when the user has got to the end of the FindNext loop?
-
Dec 19th, 2002, 09:52 AM
#8
LoL, took me 10 minutes to type that, why didnt i copy it??
I agree with jordan, have a Find and Find Next Button...alot easier..
Yes, in most applications, Find Next is visible at the same time as Find...
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 20th, 2002, 02:10 PM
#9
Thread Starter
Hyperactive Member
Hey Guys,
I can't seem to get this thing to work. Can anybody fix this code and post a working copy? I keep getting errors and as I try to fix one, I get another. What I would like is a code that allows me to search a rich text box and find EVERY occurrence of a string and not miss any. I need it to stop at each find and turn it red. It would be nice to give the total amount of finds at the beginning of the search and then count down as the we go. I know that this is simple for some of YOU guys. It is just hard for me. I will never come close to being a rocket scientist, but I love to program. This stuff fascinates me. Thanks in advanced.
Last edited by GARY MICHAEL; Dec 23rd, 2002 at 06:05 AM.
Thanks,
GARY
-
Jan 24th, 2003, 12:55 AM
#10
Lively Member
how abour searching back from a database
Dear all,
Just wondering what if I would like to seach back my data in the sql database. At present what I did was I have created some forms where the user will enter the information in the textbox and submit the data to be saved in the sql database server. What I want is for the user to be able to search back the data by using a unique key for example. Is that possible? Please advice..thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|