Wintertree Software Inc.

WSpell ActiveX Spell Checker for Visual Basic

Home Site index Contact us Catalog Shopping Cart Products Support Search

You are here: Home > Add a spell checker to your applications > Visual Basic > Using WSpell > How to check rich-text controls and TX Text Control in the background


How to check rich-text controls and TX Text Control in the background

To check spelling the background, your application must detect when the text control has been modified and when the caret (insertion point) changes. For rich-text controls, handle the Change (EN_CHANGE) and SelChange (EN_SELCHANGE) events. For TX Text Control, handle the Change (TN_CHANGED) and PosChange (TN_POSCHANGE) events. For both controls types, also handle the GotFocus (WM_SETFOCUS) events. In the handler for each of these events, call WSpell's CheckBackgroundNotify method. This method tells WSpell that something has happened in the text control that might change the spelling state of words in the control (for example, the user may have finished typing a word, so the spelling of that word can be checked). You don't need to be concerned with whether the event actually signifies something important as far as spelling is concerned; WSpell will make that determination.

Here's an example showing a call to CheckBackgroundNotify from a SelChange event from a rich-text control:

Private Sub RichTextBox1_SelChange()
Call WSpell1.CheckBackgroundNotify(RichTextBox1.hwnd, False, False, _
False, True, vbRed)
End Sub

Marking misspelled words

The CheckBackgroundNotify method accepts several parameters which tell WSpell how to mark misspelled words. Misspelled words can be marked by any combination of:

Marking signals to the user that a word was misspelled, but it is also internally used by WSpell to indicate previously detected misspelled words. For example, if the user changes a marked word, WSpell rechecks the word's spelling and unmarks it if the word is now correctly spelled. For this reason, it is important that the same text attributes used for marking misspelled words are used consistently. If some words are marked by underlining, for example, then the marking method is changed to italics, WSpell will no longer recognize underlined words as marked. One effect of this is that WSpell will not unmark underlined words if the user corrects them. (Your application may provide features in its user interface which would permit the user to remove the underline, thus unmarking the word manually.) Your application may allow the user to select the method used to mark misspelled words. If the user changes the marking method at a point when the text control contains text, and potentially marked misspelled words, you should be aware that doing so will prevent WSpell from recognizing existing marked words. The only means WSpell has to detect previously marked words is through parameters passed to the CheckBackgroundNotify and other background-related methods.

Another important point to consider is that any text in the control which has the attributes of marked text will appear to be marked by WSpell. For example, if misspelled words are marked by changing their color to red, then any red words will appear to WSpell to be marked, even if the red text color was set manually by the user. WSpell may "unmark" the word by changing its color to black. Here are some ideas to prevent or minimize this:

(In TX Text Control, the situation is somewhat simpler, since marked words can be indicated by a red zig-zag underline, which wouldn't normally be set manually by the user.)

Simply calling the CheckBackgroundNotify method from the change and caret-change event handlers will result in the following:

Context menu

WSpell includes a context menu feature that displays a small pop-up menu over a misspelled word. The menu contains suggested replacements for the word, plus items to ignore the word (i.e., consider it correctly spelled) and to add the word to a user dictionary (the first dictionary listed in the UserDictionaryFiles property; if the UserDictionaryFiles property is empty, the "add" item will not appear). A convenient mechanism for displaying the context menu is in response to a right mouse button click over the word. A right mouse click is often used for a standard context menu, however, so you might want to require something more elaborate, such as a shift-click. You might even display the context menu if the mouse hovers over a word for a specific period of time.

To display the context menu, call the CheckBackgroundMenu method. The x and y parameters passed to this method are the position in client-coordinate pixels (i.e., relative to the upper-left corner of the text control) of the mouse, which is presumably located over a marked word. If the word under the indicated position is not marked or not over a word, the CheckBackgroundMenu method does nothing. Here is how the context menu can be displayed in response to a right mouse-button click:

Private Sub RichTextBox1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (((Button And vbRightButton) <> 0) And (Shift = 0)) Then
' Right mouse clicked. The mouse position is specified in twips.
' Translate to pixels.
Dim xPos As Integer
Dim yPos As Integer
xPos = X / Screen.TwipsPerPixelX
yPos = Y / Screen.TwipsPerPixelY
Call WSpell1.CheckBackgroundMenu(RichTextBox1.hwnd, xPos, yPos, 0, True, _
False, False, False, vbRed)
End If
End Sub

The sample code above is for Visual Basic 6.0, which provides the mouse position to the event handler in "twips," so it's necessary to convert to pixels before calling CheckBackgroundMenu. Other programming languages may already provide the position in pixels, so conversion isn't necessary. On the other hand, some programming languages may provide the mouse position in pixels but in screen coordinates, in which case it is necessary to convert the position to client coordinates.

Note that the CheckBackgroundMenu method requires marking parameters similar to the CheckBackgroundNotify method. The same marking parameters should be passed to CheckBackgroundMenu as are passed to CheckBackgroundNotify, for reasons explained above.

More on marking misspelled words

Note that if the text control's contents are saved to disk, any marked words will be saved as well. If the contents are later reloaded, marked words will still be marked. The same marking method should be used if the control is background checked as were used before the control's contents were saved, or the previously marked words will not be recognized as such.

If a document which has not been background checked previously is opened (e.g., the document may have been created by a different application, which (unfortunately) does not use WSpell), misspelled words will not be marked. You can use WSpell's CheckBackgroundRecheck method to check the existing contents of a text control to mark any misspelled words and unmark any correctly spelled words which appear to be marked.


Home Site index Contact us Catalog Shopping Cart Products Support Search


Copyright © 2015 Wintertree Software Inc.