Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created August 17, 2017 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/5d8a3b1de9171319325186441c12f8de to your computer and use it in GitHub Desktop.
Save bjoerntx/5d8a3b1de9171319325186441c12f8de to your computer and use it in GitHub Desktop.
string sLastStoredWordText;
private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
// delimiter array
char[] cDelimiters = new char[] { ',','.','\r','\t',' ' };
// check, if a delimiter has been pressed and if
// misspelled words have been found
if (!cDelimiters.Contains(e.KeyChar) ||
textControl1.MisspelledWords == null ||
textControl1.MisspelledWords.Count == 0)
return;
int iTextPosition = textControl1.InputPosition.TextPosition;
// find a misspelled word at the input position
MisspelledWord mwLastWord =
textControl1.MisspelledWords.GetItem(iTextPosition);
// if the found word is not at the input position
// store the last word and return
if (mwLastWord.Start + mwLastWord.Length != iTextPosition + 1)
{
sLastStoredWordText = mwLastWord.Text; return;
}
// if the found word is the last word, store it and return
// this prevents an auto replacement for intentionally wrong words
if (mwLastWord.Text == sLastStoredWordText)
{
sLastStoredWordText = mwLastWord.Text; return;
}
// store the last word
sLastStoredWordText = mwLastWord.Text;
// create the suggestions, we only need the first
txSpellChecker1.CreateSuggestions(mwLastWord.Text, 1);
// if suggestions are found, replace the word with the suggestion
if (txSpellChecker1.Suggestions.Count > 0)
{
e.Handled = true;
string sSuggestion = txSpellChecker1.Suggestions[0].Text + " ";
textControl1.MisspelledWords.Remove(mwLastWord, sSuggestion);
textControl1.Select(mwLastWord.Start + sSuggestion.Length, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment