Click here to Skip to main content
15,749,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How can I stop this code when grammar checking is completed? Here is the code
I have tried to use the Do Until condition but it is not stopping. It just stands there, and it is forced to enter into an infinite loop.

    While nextCheckIndex = 0
    Do Until nextCheckIndex = replacements.Count
            nextCheckIndex += 1

    End While
End Sub
Updated 25-May-16 22:33pm
Sergey Alexandrovich Kryukov 21-Dec-15 14:09pm    
"Stop this code" — what does it mean (terminate the application, exit some method — what?)?
[no name] 21-Dec-15 14:44pm    
Padorn? What are you saying?
Sergey Alexandrovich Kryukov 21-Dec-15 14:53pm    
Which part of my question is not clear?
[no name] 21-Dec-15 15:02pm    
I just need someone to help me make this code better. That function is meant for grammar checking by using Keys.ElementAt(nextCheckIndex) from the dictionary, and use the foundIndex to suggest words that will be replaced on the contextMenu.
Sergey Alexandrovich Kryukov 21-Dec-15 15:11pm    
Why you are not answering any of my two questions? Note: this is the third question.

Firstly, my apologies to the other members for posting this a solution - it's too long for a comment.

You have posted several questions regarding this project. Lately...
Member 11788899 wrote:
Chill60, please help with some code?
The short answer is that I'm not prepared to spend large amounts of my time writing code for you. I doubt you will find anyone else here who will.

However, I will give you some sound advice on the approaches you should take.

1. Throw away the code you already have for CheckForReplacementText

2. Decide on what you are actually trying to do - either drive the check from the contents of the RTB word by word OR drive the check from the list of replacements that you have.

3. Do NOT write a recursive procedure. This means that if somewhere in your CheckForReplacementText routine you write CheckForReplacementText() you are writing a recursive routine. I say again, don't.

4. ContextMenuStrip is the wrong way to present the suggested replacements to the user - you will get (have already got) yourself in a right mess trying to handle the response from it.

5. Do use a pop-up form by using ShowDialog - e.g. have a borderless form with only a ListBox that you populate, a property that exposes which item was selected and a public method to populate the list. E.g.
Public Class Suggestions
    Public Property Action As String
    'Form has FormBorderStyle = None
    Public Sub PopulateMe(suggests As List(Of String))
        'avoid the Items collection cannot be modified when the DataSource property is set.
        Dim fullList As List(Of String) = suggests.ToList()
        If suggests.Count = 0 Then
            fullList.Add("No Suggestions")
        End If
        fullList.Add("Cancel checking")

        lstSuggestions.DataSource = fullList
    End Sub
    Private Sub lstSuggestions_DoubleClick(sender As Object, e As EventArgs) Handles lstSuggestions.DoubleClick
    End Sub
    Private Sub lstSuggestions_KeyDown(sender As Object, e As KeyEventArgs) Handles lstSuggestions.KeyDown
        If e.KeyValue = Keys.Enter Or e.KeyValue = Keys.Return Then
        End If
        If e.KeyValue = Keys.Escape Then Close()
    End Sub
    Private Sub HandleSelection()
        If lstSuggestions.SelectedValue.ToString().Substring(1, 1) = "-" Then
            Exit Sub
        End If
        Action = lstSuggestions.SelectedValue
    End Sub
End Class
Which you can call from within your loop like this
Dim f As Suggestions = New Suggestions()
Dim result = f.ShowDialog(RichTextBox1)
If result = DialogResult.Cancel Or f.Action = "Cancel checking" Then
    Exit Sub
End If

6. Start with a small amount of text and a small number of replacements - it will be easier to Debug

7. Which leads me to the point - learn how to debug - see this CP article for more information on this skill Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

8. Finally, really give some thought to how you are suggesting your replacements ... consider the text "How to fool someone" - you might want to suggest "confuse" for fool here used as a verb. But then what about "She cooked a raspberry fool" - here "fool" is a noun and you might want to suggest "dessert". But "fool" is also a noun in the sentence "He is a fool" - where you might want to suggest "idiot".
One way around this is to store these in a Dictionary(Of String, List(Of String)) rather than just a
Dictionary(Of String, String)<br />
Dim replacements As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))()
replacements.Add("fool", New List(Of String)({"confuse", "idiot", "dessert"}))
replacements.Add("unknownword", New List(Of String)({"no suggestions"}))

Over to you. If you have a genuine problem with code that you have written then please come back with more questions. We will be more than happy to help - if you help yourself first.
Share this answer
[no name] 22-Dec-15 9:26am    
This is the answer I was looking for. I will write another code.
Sergey Alexandrovich Kryukov 22-Dec-15 10:10am    
Please see my comments to the question and these MSDN pages:[^],[^].

See also the syntax and semantics of do … until construct you are using already:[^].

I'm sorry if it won't immediately help you. In this case, there is only one way: learn the very basics of programming.

Share this answer
[no name] 21-Dec-15 15:53pm    
You don't code? But is atleast answer...
Sergey Alexandrovich Kryukov 21-Dec-15 16:32pm    
CHill60 22-Dec-15 8:25am    
*Sigh* indeed. I've countered the Downvote you received.
I have also posted a "solution" that consists mainly of just advice.
Sergey Alexandrovich Kryukov 22-Dec-15 10:10am    
Thank you.
[no name] 22-Dec-15 9:13am    
What then can I do if I cannot implement your advice? Vote it? Am not being rude, but tell me what you would do?

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