Results 1 to 40 of 82

Thread: Adding a dictionary instead of a list

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Adding a dictionary instead of a list

    Ok, am trying to replace bad words with Good words from a RichTextBox from a dictionary file. The program as it is, can only do it from a preloaded list. How do you do this in Visual Basic? Press F8 to run the program to see what it can do.

    This is a grammar checker. Check my file from this link.

    Dictionary wordlist



    Code:
    Public Class Form1
        Friend WithEvents RichTextBox1 As New RichTextBox With {.Dock = DockStyle.Fill}
        Friend WithEvents ReplaceMenu As New ContextMenuStrip
    
        Private replacements As New Dictionary(Of String, IEnumerable(Of TextReplacement))
        Private nextCheckIndex As Integer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(RichTextBox1)
            replacements.Add("a bad advice", {New TextReplacement("a bad suggestion", "Replace advice with suggestion."),
                                          New TextReplacement("some bad advice", "Replace a with some.")})
            replacements.Add("irregardless", {New TextReplacement("regardless", "Irregardless is not a word."),
                                          New TextReplacement("in spite", "Reword the phrase.")})
            RichTextBox1.Text = "You provided a bad advice, irregardless of your intention. You provided a bad advice, irregardless of your intention. "
        End Sub
    
        Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
            If e.KeyCode = Keys.F8 Then
                nextCheckIndex = 0
                CheckForReplacementText()
            End If
        End Sub
    
        Private Sub CheckForReplacementText()
            If nextCheckIndex = replacements.Count Then
                MessageBox.Show("Check complete.")
            Else
                Dim checkWord = replacements.Keys.ElementAt(nextCheckIndex)
                Dim foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
                If foundIndex > -1 Then
                    ReplaceMenu.Items.Clear()
                    For Each replacement In replacements(checkWord)
                        With ReplaceMenu.Items.Add(replacement.Text, Nothing, Sub(sndr As Object, ea As EventArgs)
                                                                              RichTextBox1.SelectedText = replacement.Text
                                                                              CheckForReplacementText()
                                                                          End Sub)
                            .AutoToolTip = True
                            .ToolTipText = replacement.Reason
                        End With
                    Next
                    ReplaceMenu.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))
                Else
                    nextCheckIndex += 1
                    CheckForReplacementText()
                End If
            End If
        End Sub
    End Class
    
    Public Class TextReplacement
        Public Property Text As String
        Public Property Reason As String
        Public Sub New(replacementText As String, replacementReason As String)
            Text = replacementText
            Reason = replacementReason
        End Sub
    End Class
    Last edited by nqioweryuadfge; Dec 11th, 2015 at 08:16 AM. Reason: Code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width