ComponentOne Spell 8.0 for ActiveX
Step 3: Provide As-You-Type Spell Checking (Top Panel)

Double-click the first textbox and add the following code to the Text1_GotFocus event:

Example Title
Copy Code
Private Sub Text1_GotFocus(Index As Integer)

 

    ' When moving the focus, start checking the control that has it.

    VSSpell1.CheckTyping Text1(Index).hWnd

    VSSpell1.IntegerTag = Index

    Shape1.FillStyle = 1

End Sub

The code starts by connecting the VSSpell control to the textbox that has the focus. This is enough to provide basic As-you-type Spell Checking. It also saves the index of the currently active textbox into the VSSpell1 IntegerTag property, and makes the Shape1 control transparent to indicate we haven't detected any errors yet.

By default, the VSSpell control will beep and underline misspelled words. Our demo will provide additional functionality. We will show a red sign when a bad word is detected and, optionally, display a list of suggestions. To do this, add the following code to the VSSpell1_TypingError event:

Example Title
Copy Code
Private Sub VSSpell1_TypingError(ByVal SelStart As Long, ByVal SelLength As Long, Cancel As Integer)

 

    ' Error detected. Show red light to indicate an error was detected.

    Shape1.FillColor = vbRed

 

    ' Use a solid FillStyle.

    Shape1.FillStyle = 0

   

    ' If AutoCorrect is not on, we're done.

    If Check1.Value = 0 Then Exit Sub

   

    ' Use the CheckWord property to build a list of suggestions.

    VSSpell1.BadWordDialog = vsspellNoDialog

    VSSpell1.Suggest = True

    VSSpell1.CheckWord = VSSpell1.CheckWord

   

    ' If there are no suggestions, just quit.

    If VSSpell1.SuggestionCount = 0 Then Exit Sub

   

    ' If there is only one suggestion, assume it's okay. Replace it automatically and cancel the error.

    If VSSpell1.SuggestionCount = 1 Then

        Text1(VSSpell1.IntegerTag).SelStart = SelStart

        Text1(VSSpell1.IntegerTag).SelLength = SelLength

        Text1(VSSpell1.IntegerTag).SelText = VSSpell1.Suggestion(0)

        Cancel = True

 

        ' The error has been fixed.

        Shape1.FillColor = vbGreen

        Exit Sub

    End If

   

    ' If there are multiple suggestions, build a menu.

    Dim i As Integer

    For i = 1 To 5

        mSuggest(i).Visible = False

    Next

    mSuggest(0).Caption = VSSpell1.CheckWord & " (not in dictionary)"

    For i = 1 To VSSpell1.SuggestionCount

        If i > 5 Then Exit For

        mSuggest(i).Caption = VSSpell1.Suggestion(i - 1)

        mSuggest(i).Visible = True

    Next

 

    ' When the menu is ready, display it at the  current caret position. The menu command will save the user's selection in the Tag property.

    ' Note: the code subtracts the form's Left and Top properties from the CaretPosX and CaretPosY properties to convert screen into form coordinates.

    VSSpell1.Tag = ""

    PopupMenu mPopup, , VSSpell1.CaretPosX - Left, VSSpell1.CaretPosY - Top

    If Len(VSSpell1.Tag) = 0 Then Exit Sub

       

    ' Replace the bad word with the user selection.

    Text1(VSSpell1.IntegerTag).SelStart = SelStart

    Text1(VSSpell1.IntegerTag).SelLength = SelLength

    Text1(VSSpell1.IntegerTag).SelText = VSSpell1.Tag

 

    ' The error is corrected!

    Cancel = True

    Shape1.FillColor = vbGreen

   

End Sub

This routine is pretty long, but it's fairly simple. If the AutoCorrect option is off, it simply shows the red shape to indicate that a word was misspelled and allows the VSSpell1 control to provide the default user-feedback actions (beep and underline the offending word).

If AutoCorrect is on, the routine uses the VSSpell1 CheckWord property to build a list of suggestions. If VSSpell1 cannot provide any suggestions, the routine returns immediately. If a single suggestion is provided, the code replaces the offending word with the suggestion automatically. This is probably not a great idea in practice, but it's interesting to watch as the control corrects words automatically. Finally, if many suggestions are available, the control assembles them into a pop-up menu and prompts the user to select one of the options. In the demo, we only use the first five suggestions.

To make the pop-up menu work, we need to implement the menu-handling function. All it needs to do is set the VSSpell1 Tag property to the word selected by the user:

Example Title
Copy Code
Private Sub mSuggest_Click(Index As Integer)

   

    ' Index zero is whatever the user typed. Remember it so we don't pop up again on this word.

    If Index = 0 Then

        VSSpell1.Tag = ""

        VSSpell1.IgnoreAllWord = True

        Exit Sub

    End If

   

    ' Place the corrected text in Tag property.

    VSSpell1.Tag = VSSpell1.Suggestion(Index - 1)

   

End Sub

We're almost done now. The only thing missing is the code to hide the red shape when the user types in a word that is correct. This can be done easily using the VSSpell1_TypingOK event.

Example Title
Copy Code
Private Sub VSSpell1_TypingOK(ByVal SelStart As Long, ByVal SelLength As Long)

 

   ' If typed correctly, erase the red light.

    Shape1.FillStyle = 1

End Sub

That's it for the top pane on our demo. Save the project and run it. Start typing into the top three textboxes and you will see VSSpell's As-you-type spell checking in action. Here's how the main form looks when an error is found:

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback