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 edit controls in the background


How to check edit controls in the background

Standard edit controls and text boxes, used for general entry and editing of unformatted text, can be background checked with some limitations. These controls do not support altering the attributes of displayed text, so marking misspelled words is not possible. For example, it is not possible to indicate misspelled words by underlining, as edit controls do not support underlining. However, it is still possible to detect misspelled words as they are typed. WSpell can indicate to your application that a misspelled word was just typed, and your application can alert the user in some way.

To check spelling in the background, your application must detect when the edit control has been modified. Handle the Change (EN_CHANGE) event, and in the event handler call WSpell's CheckBackgroundNotify method. This method tells WSpell that something happened in the edit control that might change the spelling state of the word being typed. For example, the user may have finished typing the word, so the spelling of the word should 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.

The CheckBackgroundNotify method returns 1 if a misspelled word was typed, and 0 if no misspelling was detected (which might mean the last word typed was not misspelled, or a complete word has not yet been typed). If CheckBackgroundNotify returns 1, your application can alert the user in some way. The method used for alerting will depend on your application and its users, but might include:

Here's an example showing how to change the background color of the edit control in response to a misspelled word:

Private Sub Text1_Change()<BR> If (WSpell1.CheckBackgroundNotify(Text1.hwnd, False, False, False, False, 0)) Then<BR> ' A misspelled word was detected.<BR> Text1.BackColor = vbYellow<BR> Else<BR> Text1.BackColor = vbWhite<BR> End If<BR>End Sub

Note that this approach with CheckBackgroundNotify can be used to detect only that the last word typed was misspelled. It can't be used to detect that a previously correct word was made misspelled by moving the caret to it and editing, nor can it be used to detect that a misspelled word was corrected. When CheckBackgroundNotify returns 0, the only thing you can infer is that a misspelled word was not typed. You can't infer that a word was corrected, or that the word near the caret is correctly spelled. The only significant notification from CheckBackgroundNotify is a return value of 1, which means that the word just typed was misspelled. These limitations result from the inability of edit controls to generate caret-change events and to mark misspelled words.

Context menu

When the user types a misspelled word, your application can display a spelling context menu by calling the CheckBackgroundMenu method. The CheckBackgroundMenu method displays a menu containing suggested replacements for the misspelled word, plus items to ignore all occurrences of the word (i.e., treat the word as 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). You might display the context menu

The x and y parameters passed to CheckBackgroundMenu 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 an example showing how to display the context menu in response to a left mouse-button click:

Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (((Button And vbLeftButton) <> 0) And (Shift = 0)) Then
' Left 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(Text1.hwnd, xPos, yPos, 0, False, _
False, False, False, 0)
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.

In situations where the context menu is displayed in response to an event that is not mouse-related, it may be more convenient to indicate the misspelled word based on its position in the text control rather than its coordinates. You can do this by passing -1 as the x parameter and the position (offset) in characters of the word in the y parameter.Here's an MFC example showing how to display the context menu whenever a misspelled word is detected:

void CCheckBackgroundDlg::OnChangeEdit1(){
CEdit *edit1 = (CEdit *)GetDlgItem(IDC_EDIT1);
if (m_WSpell.CheckBackgroundNotify((long)edit1->m_hWnd,
FALSE, FALSE, FALSE, FALSE, 0)) {
// Misspelled word detected.
// Display the spelling context menu automatically.
int selStart, selEnd;
edit1->GetSel(selStart, selEnd);
// selStart -1 is the position of the previous character, which
// should be the last character of the misspelled word. The
// misspelling isn't detected until a word separator is typed.
CPoint pos = edit1->PosFromChar(selStart - 1);
m_WSpell.CheckBackgroundMenu((long)edit1->m_hWnd, -1, selStart - 1,
0, FALSE, FALSE, FALSE, FALSE, 0);
}
}

The character position passed in the y parameter can be anywhere within or adjacent to the misspelled word.

Note that the CheckBackgroundMenu method will not display the context menu if the word is not misspelled.


Home Site index Contact us Catalog Shopping Cart Products Support Search


Copyright © 2015 Wintertree Software Inc.