SpellCheck Class In Code Behind

不打扰是莪最后的温柔 提交于 2019-12-06 07:18:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!