Searching a string-giving user control of the search
I am trying to learn how to use the InStr function in VB6. As you can see by the code below I can search the main string for each occurrence of the substring and then highlight each substring occurrence. I learned this from some grateful help from Phil and Mstraf. However, the only way I know of to see each substring highlighted is to use the For-Next loop to slow down the Do-Loop, as the Do-While loop loops too quickly. I know this is not good code, but I’m at a loss as to what other ways are available.
What I want to do is have the program search the main string for each occurrence of the substring, highlight each substring occurrence and then stop the loop after each substring occurrence to allow the user to decide to continue the search or not by answering the Yes/No in the message box.
For example, lets say there are 4 substrings within the main string I'm searching through. I want my program to find the 1st substring and stop at it to allow the user the chance to review this first substring and then decide to either continue searching or to end searching; the reason this is important is because if the main string is long (lets say it is a letter), it would be useful for the user to be able to see the substring within the context of the letter. If the substring was "an" the program could find the "an" in the word 'and' or it could find the "an" in the name 'Anne'. So, now the user has reviewed the 1st substring and decides to move on to the 2nd substring, the user would have to click the YES button to continue the search for the 2nd substring. Now, lets say that the user decides to end the search after reviewing the 2nd substring, so they would have to click the NO button to exit. If, however, the user chose to continue the search, he would click the YES button and the search would continue until the 3rd substring is found. Again, it would stop at the 3rd substring, waiting for the user to click YES or NO.
With regard to the For-Next loop, I merely used it to slow down the looping of the Do-While loop. The Do-While loop loops so quickly that I couldn't see each substring being highlighted. So, using the example above, the 1st substring would be found and highlighted (with a short pause) and so on to the last (4th) instance of the substring. The For-Next loop creates the short pause as each substring is found.
The Microsoft Note Pad program that comes with Windows is a good example of what I am trying to do with my code. In Note Pad the user can search the text (main string) for any sub text (substring) using the Find command under the Edit menu. When the first occurrence of the substring is found, it is highlighted and the user has the option to continue searching or to quit searching. If the user chooses to continue searching the next occurrence of the substring is found and highlighted and again the user has the option of continuing to search or to quit. This is what I want my program to do.
Can anyone out there help me with this?
Private Sub cmdFind_Click()
Dim substring As String
Dim found As Long
substring = "an"
Text1.Text = "dan and janie and dan and janie"
Do
found = InStr(found + 1, Text1.Text, substring)
If found Then
Text1.SetFocus
Text1.SelStart = found - 1
Text1.SelLength = Len(substring)
resp = MsgBox("Continue?", vbYesNo)
For t = 1 To 50000000: Next
End If
Loop While found
End Sub
Reply to Ron and Mstraf-more information
Ron, thank you for suggesting the code that you did. I realize that I must not be describing very well what I want my program to do. The code you suggested does exactly what mine did, all I did was add the For-Next loop to “show” the highlighting of the substring a bit longer. And, it only showed the highlighting for a second or two, not very much longer than the code without the For-Next loop, but long enough to at least see the “an” actually highlighted. Without the For-Next loop the highlighting was so quick that I could hardly see the substring being highlighted. I’d like my program to search for the 1st substring, highlight it and have it REMAIN HIGHLIGHTED until the user clicks the ‘Yes’ button (in a perfect world, the user could go get a cup of coffee and come back and it would still be highlighted). It would then continue searching until it found the 2nd instance of the substring and then highlighted it and have it REMAIN HIGHLIGHTED until the user clicks the ‘Yes’ button…and on and on until there were no more instances of the substring.
The crux of my problem it seems is that the Instr function works well, however it only highlights the substring for a FRACTION OF A SECOND. It does not remain highlighted. I typed in your code exactly as you suggested. When I run the program and look carefully I can barely see the substring “an” being highlighted. It flashes for a very brief moment only. Is there a way to have the substring highlighted and REMAIN highlighted until the user clicks ‘Yes’? As I said in my post, the best example of what I want my program to do is the Find command in the Windows Note Pad.
And Mstraf, thank you too for your comments.
P.S. I don’t understand what the text1.HideSelection property does and why is it needed. I remarked it out and the program worked the same way. And in the MsgBox it says that the read only property must be set to false. The code sets it to True. I don’t understand. The HideSelection property is a brand new concept for me. I’d truly be appreciative if you, or a link you could provide me, would help me understand it and its purpose.
Thank you, both, again for your suggestions.
Ron, your code works-I forgot to set Hide Selection property
Hi Ron and Mstraf,
Ron, your code works-I forgot to set the Hide Selection property for text1.text box; I appreciate your help. And Mstraf, I entered your suggestion and got a compile error: Can't assign to read only property. So, I just remarked it out. Thank you for your suggestions.
Again, thanks to you both. You've helped me learn. Now, I just have to analyze each line of code to make sure I understand what is happening. I hope in the future, if I have a problem you guys will help me again.