-
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
-
I do not understand.
The MsgBox stops the execution flow until the user clicks the Yes or No button, so I do not see why you have to slow down the process with the inner loop.
Anyway, if the user clicks on No the loop must exit:
if MsgBox("Continue?", vbYesNo) = vbNo then exit do
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
Try this code:
Code:
Private Sub Form_Load()
'preload search value to "an"
Text2.Text = "an"
'preload the search text when the form opens, that is, before the find button is pressed
'this way the user can change the text to be searched before starting the find operation
Text1.Text = "dan and janie and dan and janie"
End Sub
Private Sub cmdFind_Click()
Dim substring As String
Dim found As Long
substring = Text2.Text
If substring = "" Then substring = "an"
'the HideSelection Property can only be set at DesignTime
If Text1.HideSelection = True Then
MsgBox "ReadOnly Property 'HideSelection' must be set to 'False' or the " & vbCrLf _
& "Search Value will only be highlighted when the text box has the focus.", vbOKOnly + vbInformation
End If
Do
found = InStr(found + 1, Text1.Text, substring)
If found Then
Text1.SetFocus
Text1.SelStart = found - 1
Text1.SelLength = Len(substring)
If MsgBox("Continue?", vbYesNo + vbInformation) = vbNo Then Exit Sub
Else
Text1.SelStart = Len(Text1.Text)
Text1.SelLength = 0
MsgBox "No More """ & substring & """ Found!", vbInformation + vbOKOnly
Exit Sub
End If
Loop While (found > 0 And found <= Len(Text1.Text))
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.
-
Not so! It stays highlighted the entire time and it now actually lets you
cancel the search, which did not work before.
I think the part you missed was this:
Code:
'the HideSelection Property can only be set at DesignTime
If Text1.HideSelection = True Then
MsgBox "ReadOnly Property 'HideSelection' must be set to 'False' or the " & vbCrLf _
& "Search Value will only be highlighted when the text box has the focus.", vbOKOnly + vbInformation
End If
If you don't set the HideSelection Property to False then you won't see the
highlight when the text box no longer has focus.
Last edited by Ron Weller; 05-30-2008 at 10:18 PM.
-
Ron is right, now I understand the problem.
put this instruction in the form Load event
Text1.HideSelection = True
or set it in the designer. Basically that property tells the selection to remain highlighted even when the text box loses the focus (that happens right away as soon as the msgvox is displayed)
sorry I did not understand from the beginning...
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
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.
Similar Threads
-
By ObiWan in forum VB Classic
Replies: 3
Last Post: 05-23-2006, 10:35 AM
-
By Rob Teixeira in forum .NET
Replies: 15
Last Post: 05-31-2002, 03:30 PM
-
By Todd W in forum ASP.NET
Replies: 1
Last Post: 03-30-2002, 01:14 AM
-
By Martin in forum VB Classic
Replies: 22
Last Post: 12-03-2001, 04:53 AM
-
By Fred Mayes in forum Java
Replies: 1
Last Post: 06-05-2001, 06:12 AM
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
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|