Saturday, February 06, 2010

Spell check enable your WinForm TextBoxes/RichTextBoxes via a nHunspell based IExtenderProvider

CodeProject - NHunspellTextBoxExtender - A Spellchecking IExtenderProvider for TextBoxes using Hunspell for .NET



With many applications, spell checking can be a vital aspect to include. Most people are used to the spell checking capabilities of products like Microsoft Word or OpenOffice. There are products available for purchase that can add spell checking capability, such as SharpSpell that can cost hundreds of dollars. But, there is a lack of Open Source, freely available tools that can provide the functionality of Microsoft Word. That is why I began to work on a spell checking IExtenderProvider that could extend any control that inherits TextBoxBase (both TextBox and RichTextBox inherit TextBoxBase).


… I wanted to provide spell checking capability to my GUI application. There were plenty of ways to spell check text, from using NetSpell, to programmatically using Microsoft Word's spell checker in a way similar to this article. Not every user could be assured to have Microsoft Word, and opening a new Word application can take some time and uses resources, so I wanted to stay away from that. I chose instead to use NHunspell. A useful article on it can be found at: hunspell-for-net.aspx.

I also wanted to provide a visual cue to the user that there was a spelling error. With RichTextBoxes, this could be done through simple underlining, such as in this article. My problem was that I had written a lot of code using simple textboxes, and I didn't want to change all of them to RichTextBoxes. Instead, I wanted to use the IExtenderProvider to extend any textbox with spell checking capabilities. To be honest, I had no clue where to even begin to do that. That is, until I found this blog that describes exactly how to draw that wavy red line on a textbox. With that base code, I was able to use NHunspell to determine where to draw the line.

Exploring the code

IExtenderProviders can be very useful, and most of us that code use them regularly, maybe even without knowing it. The simplest example is the ToolTip. When you add a ToolTip to a form, it doesn't show up as a control directly on the form. And, while it has its own properties, it also adds

image …”

I thought the number of things that this article brings together was pretty cool. That and the fact that it’s VB… :)


Related Past Post XRef:
NHunspell - 0.9.2 released and two cool C# & VB.Net Code Projects too
Hunspell (Open Office’s Spell Checker) wrapped for .Net = NHunspell – Your LGPL spell checker, hyphenation library for .Net


Anonymous said...

how the spellcheckform is added to any project?

Greg said...

@Anonymous Click through to the article, that should provide all the details, examples, samples, etc