Spell Checking
A red zigzag line under a word tells the user that it is misspelled. TXTextControl draws that line, counts the misspelled words and replaces them with corrections, but it does not decide which words are wrong: since version 16.0, it hands the changed text to your code, and your code asks a spell checker and reports the misspelled words back.
What follows explains this interface, fills it with a simple check of your own and then with TX Spell .NET, the spell checker of Text Control, through the TX Spell .NET ActiveX Package. The last sections correct misspelled words from a context menu and from a spelling dialog box of your own.
How Spell Checking Works
TX Text Control ActiveX has no dictionary of its own. Its spell checker interface lets you connect any spell checker to a TXTextControl, and it works in three steps:
- You switch spell checking on with the EnableSpellChecking property.
- Each time the text of the document changes, the control raises the SpellCheckText event. Its
Textparameter holds the text to check. - Your event handler checks the text and returns the positions of the misspelled words in the
MisspelledWordPositionsparameter.TXTextControlunderlines these words with a red zigzag line.
MisspelledWordPositions is an array of Long values that your handler creates. It holds two entries for each misspelled word, the start position and the end position, and both are one-based indexes into Text. For the text "This iss an exxample", with the misspelled words "iss" and "exxample", the array holds the four values 6, 8, 13 and 20.
After that, the control keeps track of the misspelled words. Your code reads their number with MisspelledWords, their position and length with MisspelledWordStart and MisspelledWordLength, and corrects or ignores a word with MisspelledWordDelete. All of these members count the misspelled words from the beginning to the end of the document, starting with 1.
Since version 20.0, a piece of text can be formatted for a language, which spell checking and hyphenation use. The Locale property sets it for the selected text as a Windows locale identifier (LCID), and the LocaleDialog method lets the user choose it.
Turn On Spell Checking
EnableSpellChecking is a Boolean property. Set it to True in the Properties window of Visual Basic, as the samples of the TX Spell .NET ActiveX Package do, or in code, for instance when the form loads:
Private Sub Form_Load()
' Raise the SpellCheckText event when the text changes
TXTextControl1.EnableSpellChecking = True
End Sub
As long as EnableSpellChecking is False, the SpellCheckText event does not occur.
Report Misspelled Words to the Control
The following handler shows what the control expects from the SpellCheckText event. It splits the text at spaces and asks the function IsMisspelled about each word. In a real application, IsMisspelled calls a spell checker; here it knows two misspelled words, so you can try the handler with the text "This iss an exxample":
Private Sub TXTextControl1_SpellCheckText(ByVal Text As String, MisspelledWordPositions As Variant)
Dim Positions() As Long
Dim Count As Long
Dim WordStart As Long
Dim I As Long
WordStart = 1
For I = 1 To Len(Text) + 1
' A space or the end of the text ends a word
If I > Len(Text) Or Mid$(Text, I, 1) = " " Then
If IsMisspelled(Mid$(Text, WordStart, I - WordStart)) Then
ReDim Preserve Positions(Count + 1)
Positions(Count) = WordStart ' one-based start
Positions(Count + 1) = I - 1 ' one-based end
Count = Count + 2
End If
WordStart = I + 1
End If
Next I
' Report the misspelled words, if there are any
If Count > 0 Then MisspelledWordPositions = Positions
End Sub
Private Function IsMisspelled(Word As String) As Boolean
' Stand-in for a call to a spell checker
IsMisspelled = (Word = "iss" Or Word = "exxample")
End Function
The code works as follows:
- The handler declaration is the one the samples of the TX Spell .NET ActiveX Package use:
Textis passed by value as aString, andMisspelledWordPositionsis aVariantthat the handler assigns the array to. Positionscollects twoLongvalues for each misspelled word. The start is the one-based position of the word's first character inText, and the end is the position of its last character.- For "This iss an exxample", the handler returns 6, 8, 13 and 20, and
TXTextControlunderlines "iss" and "exxample".
Check With TX Spell .NET
TX Spell .NET is the spell checking component of Text Control for .NET applications, available as TX Spell .NET for Windows Forms. To use it from Visual Basic 6, Text Control released the TX Spell .NET ActiveX Package in January 2012, as the blog article TX Spell .NET ActiveX Package Released - Spell Checking in VB6 announced. It is a Visual Studio project that wraps TX Spell .NET for Windows Forms in a COM-visible control, AxTXSpellChecker, whose members Visual Basic 6 can call.
The package comes as source code, and you need TX Spell .NET for Windows Forms to build it. Since November 2012 the source has been open for changes by its users, as TX Spell .NET ActiveX Package Goes CodePlex explains; today it is the GitHub repository TXSpell.ActiveX-Package. The repository holds the wrapper project AxTXSpell and two Visual Basic 6 samples, Simple and DialogAndContextMenu. Its first_steps.md file describes how to build the wrapper in Visual Studio, register its type library with regasm and open the samples. The wrapper project references TX Spell .NET for Windows Forms 10.0 and targets .NET Framework 4.0.
With a reference to the wrapper's type library, which the samples list as TX Spell .NET ActiveX Package among their references, the whole spell checking code of the Simple sample is this:
Dim TXSpell1 As AxTXSpell.AxTXSpellChecker
Private Sub Form_Load()
Set TXSpell1 = New AxTXSpellChecker
End Sub
Private Sub TXTextControl1_SpellCheckText(ByVal Text As String, MisspelledWordPositions As Variant)
TXSpell1.Check (Text)
MisspelledWordPositions = TXSpell1.MisspelledWordPositions
End Sub
The code works as follows:
Form_Loadcreates oneAxTXSpellCheckerfor the form.EnableSpellCheckingis set to True in the Properties window ofTXTextControl1.- In the
SpellCheckTextevent, the wrapper'sCheckmethod checks the text. - The wrapper's
MisspelledWordPositionsproperty returns the start and end positions in the format that the event expects, so the handler assigns them directly.
The members of AxTXSpellChecker come from the source code of the package, not from TX Text Control ActiveX. Besides Check, the samples use CreateSuggestions and GetSuggestions for correction suggestions and OptionsDialog for the options dialog box of TX Spell .NET.
Build a Spelling Dialog Box
The second form of the DialogAndContextMenu sample, a dialog box named Spelling, goes through the misspelled words of the document one by one. It always works on misspelled word number 1: once that word has been corrected or ignored, it leaves the list, and the next word becomes number 1. The excerpt below shows the Change and Ignore Once buttons. It is adapted from the sample: it leaves out the preview of the word in its line and the list of suggestions, and it ignores a word with an empty string, as the reference describes.
Private Sub NextMisspelledWord()
If TXTextControl1.MisspelledWords = 0 Then
MsgBox "Spell checking complete."
Else
' Select the first remaining misspelled word
TXTextControl1.SelStart = TXTextControl1.MisspelledWordStart(1)
TXTextControl1.SelLength = TXTextControl1.MisspelledWordLength(1)
End If
End Sub
Private Sub btnChange_Click()
' Replace the word with the selected suggestion
TXTextControl1.MisspelledWordDelete 1, listSuggestions.Text
NextMisspelledWord
End Sub
Private Sub btnIgnoreOnce_Click()
' Keep the word's text, remove it from the list
TXTextControl1.MisspelledWordDelete 1, ""
NextMisspelledWord
End Sub
The code works as follows:
MisspelledWordsreturns the number of misspelled words in the document. When it is 0, the check is complete.MisspelledWordDeletetakes the number of the word and a corrected text. With a corrected text, it replaces the word's text. With an empty string, the word's text stays as it is, and the word is only removed from the list of misspelled words.- The method returns True when it could remove the word.
In the sample, the dialog box also sets HideSelection of the document's TXTextControl to False when it loads, so that the selected word stays visible while the dialog box has the input focus. The list of suggestions comes from the CreateSuggestions and GetSuggestions members of the wrapper.
Spell Checkers of Earlier Versions
Before the spell checker interface of version 16.0, TX Text Control ActiveX left spell checking to components of other vendors, which a special API of the control let them build for it, as the blog article Spell Checking in TX Text Control ActiveX of 2004 describes. The obsolete VTSpellCheck method and VTSpellDictionary property started the VT Speller of VisualTools, which was not part of the Text Control package and is no longer available. Use the spell checker interface described above instead.
Key Members
| Member | Description |
|---|---|
| EnableSpellChecking | Switches spell checking on or off. When it is True, the SpellCheckText event occurs. |
| SpellCheckText | Occurs when text has changed and must be checked; the handler returns the positions of the misspelled words. |
| MisspelledWords | Returns the number of misspelled words in the document. |
| MisspelledWordStart | Returns the zero-based position of a misspelled word. |
| MisspelledWordLength | Returns the length of a misspelled word. |
| MisspelledWordAtInputPos | Returns the number of the misspelled word at the current input position. |
| MisspelledWordDelete | Removes a word from the list of misspelled words and optionally replaces its text. |
| Locale | Sets the language of the selected text. |
The programming category Spell Checking lists all members of the spell checker interface.