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
Public Sub PopulateMe(suggests As List(Of String))
Dim fullList As List(Of String) = suggests.ToList()
If suggests.Count = 0 Then
fullList.Add("No Suggestions")
End If
fullList.Add("--------------")
fullList.Add("Cancel checking")
lstSuggestions.Items.Clear()
lstSuggestions.DataSource = fullList
End Sub
Private Sub lstSuggestions_DoubleClick(sender As Object, e As EventArgs) Handles lstSuggestions.DoubleClick
HandleSelection()
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
HandleSelection()
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
Close()
End Sub
End Class
Which you can call from within your loop like this
Dim f As Suggestions = New Suggestions()
f.PopulateMe(checkList)
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 />
e.g.
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.