Spell Check Tool in Visual Basic

Introduction: This tutorial is on how to create a spell check tool in Visual Basic. Dictionary: First we need to get a dictionary of some sort, I am just going to create a simple text dictionary by opening a new Notepad document and typing: they're their there Note; we put one word per line so we can easily distinguish which words are which later on within our program. You should be able to download a dictionary document from somewhere else online. Design: Our program is going to have two components; Button, button1, To start the spell check function. Textbox, textbox1, To contain the words to spell check. Reading the Dictionary: Before we can check the word spelling, we need something to check it against. So first we create a new string list to contain the dictionary words, this is globally available so don't declare it within a sub/process/function...
  1. Dim dictionary As List(Of String) = New List(Of String)
Then we open a new file reading stream using StreamReader within the System.IO package to the dictionary .txt file we created/downloaded/saved earlier, followed by adding each line (since we put one word per line) to the dictionary words string list...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.         Using sr As New System.IO.StreamReader("C:\Users\Josh\Documents\Freelance Work\JayPabs\dictionary.txt")
  3.                 While (sr.Peek <> -1)
  4.                         dictionary.Add(sr.ReadLine())
  5.                 End While
  6.         End Using
  7. End Sub
We put this process in the form load sub so that the dictionary file is read once the program is loaded and ready to go. Spell Check Function: Now we need to create a simple spell check function on the button1 click sub. This function will split the text contained within the textbox1 component by a space (" ") so we get each word within our document then we can check each one. So once we have the words as a string array, we iterate through each word and see if it is in our dictionary string array, if not, we output a simple messagebox saying that it is possibly spelt wrong - of course, it might be a noun which would not be in the dictionary and so it could still be spelt right...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.         Dim words As String() = TextBox1.Text.Split(" ")
  3.         For Each word As String In words
  4.                 Dim isReal As Boolean = False
  5.                 For Each check As String In dictionary
  6.                         If (word.ToLower() = check.ToLower()) Then isReal = True
  7.                 Next
  8.                 If Not (isReal) Then MsgBox(word + " was not found within the dictionary...")
  9.         Next
  10. End Sub
Note: If you wanted to make this tool portable, instead of hardcoding the dictionary .txt file path in to the program, you could ask the user to open the document through a new openfiledialog box, like so...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.         Dim fo As New OpenFileDialog
  3.         fo.Filter = "Text Files | *.txt"
  4.         fo.FilterIndex = 1
  5.         fo.Multiselect = False
  6.         fo.ShowDialog()
  7.         Using sr As New System.IO.StreamReader(fo.FileName)
  8.                 While (sr.Peek <> -1)
  9.                         dictionary.Add(sr.ReadLine())
  10.                 End While
  11.         End Using
  12. End Sub
  13. </vb
  14.  
  15. Or you can set the directory to the file as the current directory followed by the dictionary file name and extension (".txt"), that way you can simply put the tool .exe program with the dictionary .txt file in any directory and the tool will still find the dictionary file (as long as it has the correct name and extension)...
  16.  
  17. <vb>
  18. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  19.         Using sr As New System.IO.StreamReader(CurDir() + "\dictionary.txt").txt"
  20.                 While (sr.Peek <> -1)
  21.                         dictionary.Add(sr.ReadLine())
  22.                 End While
  23.         End Using
  24. End Sub

Add new comment