问题
Is there a way to check spelling in code behind?
I only can find how to use it with UI control
<TextBox SpellCheck.IsEnabled="True" Height="20" Width="100"/>
What I want is boolean CheckSpell(string word)
I don't even need a suggested spelling.
This would used to determine the percentage of correctly spelled words in a text file.
A text file with a real low number was probably not intended for human consumption.
The app has SQL back end so loading a list of words in the Englich dictionary is an option.
回答1:
To solve your problem you can use the NHunspell library.
Your check method in this case is very simple and looks like this:
bool CheckSpell(string word)
{
using (Hunspell hunspell = new Hunspell("en_GB.aff", "en_GB.dic"))
{
return hunspell.Spell(word);
}
}
You can find dictionaries on this site.
Also you can use SpellCheck class:
bool CheckSpell(string word)
{
TextBox tb = new TextBox();
tb.Text = word;
tb.SpellCheck.IsEnabled = true;
int index = tb.GetNextSpellingErrorCharacterIndex(0, LogicalDirection.Forward);
if (index == -1)
return true;
else
return false;
}
来源:https://stackoverflow.com/questions/14541038/spellcheck-class-in-code-behind