Click here to Skip to main content
15,749,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to use a While Loop to search for words on button click. But the contextMenu keeps on jumping up and down or loading very quickly without stopping or showing the results. Here is the code:

Private Sub CheckForReplacementText()

    If nextCheckIndex = replacements.Count Then

        MessageBox.Show("Check complete.")
        nextCheckIndex = 0

    End If

What I have tried:

To use a while loop
Updated 25-May-16 22:23pm
Matt T Heffron 20-May-16 13:37pm    
Well, you are clearing and reloading the ContextMenuStrip1.Items inside the While loop, so of course it is changing multiple times!

Why are you trying to do this recursively, when it's a loop problem? Recursion is only useful when you pass a value to the method as a parameter, so that different iterations have an independent value.
And all you do is call the same method, and exit when it returns - so a loop is a much more sensible solution here.
And if at any time nextCheckIndex is greater than replacements.Count and foundIndex is zero or more ... your code will recurse forever and you will get a stack overflow - as you have found.
Try it: use the debugger and you will see what I mean.

Don't use recursion unless it's a task which genuinely needs it, or data which exhibits a recursive structure such as processing all the files in all the subdirectories of a folder. This doesn't - it's just a string of data and you are looking for bits to change...
Share this answer
[no name] 20-May-16 9:44am    
I understand that a Loop will be better. Did you mean I use

Do While if foundIndex >-1

[no name] 20-May-16 10:08am    
OK, check the updated question and code. A new problem has presented itself.
OriginalGriff 20-May-16 10:15am    
Time to crank up the debugger and start looking at exactly what is going on! :laugh:
You know how to use the debugger, I assume? Or have they not covered that yet?
[no name] 20-May-16 10:20am    
Let me check a video on how to do that, I will get back to you!
Debug your code to step through it line by line and that will show what the problem is. What is "replacements.Count" for example? If it is 0 then your code will loop forever, but only you can know that. If that is the issue try

If nextCheckIndex = replacements.Count Or replacements.Count = 0 Then

    MessageBox.Show("Check complete.")
    nextCheckIndex = 0

End If
Share this answer
[no name] 20-May-16 9:42am    
What about telling the user "You cannot check more than that when it finishes checking?

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click

If nextCheckIndex > replacements.Count Then

MessageBox.Show("You cannot check more than that!")

ElseIf nextCheckIndex < replacements.Count Then
foundIndex += 1

End If
End Sub
[no name] 20-May-16 10:08am    
OK, check the updated question and code. A new problem has presented itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900