|
-
Mar 11th, 2001, 07:52 PM
#1
Thread Starter
Hyperactive Member
ok the following code searches a string but once carried out it will not search for a second occurence if called again ...
Dim StartPos As Long
StartPos = InStr(1, rich.Text, "cat")
''''the proble is that 1 because every time the code is called it will begin to search from the start''''
If StartPos <> 0 Then
rich.SelStart = StartPos - 1
rich.SelLength = Len("cat")
rich.SetFocus
End If
i tried making another variable to be static and hold the startpos but it doesn't work ... how do those programs have 2 options of "find" and "find next" ... how do they remember the last position without it being deleted from memory?
-
Mar 11th, 2001, 08:58 PM
#2
Fanatic Member
Using a Static variable sounds like the best idea, perhaps something like this...
Code:
Private Sub Find(ByVal str As String, ByVal pNext As Boolean)
Dim StartPos As Long
Static PrevPos As Long
If pNext = True Then
StartPos = InStr(PrevPos + 1, rich.Text, str) ' search from previous position
Else
StartPos = InStr(1, rich.Text, str)
End If
''''the proble is that 1 because every time the code is called it will begin To search from the start''''
If StartPos <> 0 Then
rich.SelStart = StartPos - 1
rich.SelLength = Len(str)
rich.SetFocus
PrevPos = StartPos ' Set the previous position equal To the current position
Else
MsgBox "Search String Not found"
PrevPos = 1 ' If search unsuccessful Then reset position
End If
End Sub
'*****IMPLEMENTATION*****
Private Sub cmdFind_Click()
Find "cat", False
End Sub
Private Sub cmdFindNext_Click()
Find "cat", True
End Sub
Hope that helps!
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Mar 13th, 2001, 06:28 PM
#3
Thread Starter
Hyperactive Member
thanks
that code was really helpful ... i tried using a static variable to hold the position but my mistake was that i tried putting it in the general declarations and so i kept getting an error ... how come a 1 needs to be added to:
StartPos = InStr(PrevPos + 1 ... ? i noticed if i take it out it only searches from the beginnning
ps ... is it necessary to use ByVal in the sub? - what is its purpose?
-
Mar 13th, 2001, 07:19 PM
#4
Fanatic Member
The reason for the + 1 is becuase the PrevPos variable holds the position in the string of the previous find. Well it's kinda hard to explain maybe an example will help, take this string....
My cat is fat, my cat is green!
When you run the Find function the first time with the word 'cat' as a search string it will return the number 4 as the first position in the string, now if you omit the + 1 it will start searching for the string at position of 4, i.e.
cat is fat, my cat is green!
and the function will find the first instance of cat over and over again but if you add the + 1 it will start at position 5 which when chopped up would look like this..
at is fat, my cat is green!
I hope that makes sense, I'm not even sure it does to me!
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Mar 13th, 2001, 07:23 PM
#5
Fanatic Member
BTW when you pass an argument by value you cannot change the variable in the sub...
Code:
Sub PlusOne(ByVal i as Integer)
i = i + 1 ' does nothing b/c i is passed by value
End Sub
Private Sub Form_Load()
Dim j as Integer
j = 5
PlusOne j
debug.print j ' prints 5
End Sub
but when you pass an argument by reference you pass the actual variable and any changes made to the variable inside the sub will affect the variable that was passed to the sub....
Code:
Sub PlusOne(ByRef i as Integer)
i = i + 1 ' adds one to whatever variable is passed as i
End Sub
Private Sub Form_Load()
Dim j as Integer
j = 5
PlusOne j
debug.print j ' prints 6
End Sub
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Mar 13th, 2001, 10:33 PM
#6
Thread Starter
Hyperactive Member
thanks again
so most functions made without those byval or byref are usually defaulted to byref right?
-
Mar 14th, 2001, 06:22 PM
#7
Fanatic Member
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
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
|