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"
found = InStr(found + 1, Text1.Text, substring)
If found Then
Text1.SelStart = found - 1
Text1.SelLength = Len(substring)
resp = MsgBox("Continue?", vbYesNo)
For t = 1 To 50000000: Next
Loop While found
By ObiWan in forum VB Classic
Last Post: 05-23-2006, 11:35 AM
By Rob Teixeira in forum .NET
Last Post: 05-31-2002, 04:30 PM
By Todd W in forum ASP.NET
Last Post: 03-30-2002, 01:14 AM
By Martin in forum VB Classic
Last Post: 12-03-2001, 04:53 AM
By Fred Mayes in forum Java
Last Post: 06-05-2001, 07:12 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL