Click here to Skip to main content
15,886,919 members
Articles / Desktop Programming / Windows Forms

i00 Spell Check and Control Extensions - No Third Party Components Required!

Rate me:
Please Sign up or sign in to vote.
4.95/5 (117 votes)
11 Jan 2014Ms-PL16 min read 1.3M   22   266   885
Simple to use, open source Spell Checker for .NET
Image 1 Anyone wishing to use this code in their projects may do so, however are required to leave a post on this page stating that they are using this.
A simple "I am using i00 Spell check in my project" will suffice.

Introduction

I wanted a spell check that I could use in .NET, so like most people would have done, I Googled. After many hours of fruitless searching, I decided to make my own; sure there are plenty of spell checkers out there, but I didn't want one that relied on 3rd party components such as Word or require Internet connectivity to work. Introducing i00 .NET Spell Check, the first and only VB.NET Spell Check written completely in VB! Not only that, it is also open source, and easy to use.

Eventually, this project progressed even further into a generic control extension plugin that provides plugins for text box printing, translation, speech recognition and dictation plus more; while also providing a simple method for users to write their own extensions.

Screenshots 

Spell check with definitions

Image 2

In-menu word definitions and Change to...

Image 3

Adding words to dictionary

Image 4

Custom content menus

Image 5

Crossword generator

Image 6

Options

Image 7

Owner draw and RTB support

Image 8

Spell check dialog

Image 9

Support for DataGridViews!

Image 10

Plugin support ... with Label plugin

Image 11

Plugin support ... with FastColoredTextBox plugin

Image 12

Implementation

To implement i00 .NET Spell Check into your project, first either:

  • Add the i00SpellCheck project to your solution and reference it (recommended)
  • Reference the i00SpellCheck.exe file that is output from this project*
  • or you can bring all of *.vb files in the "SpellCheck\Spell Check" folder (from the zip) directly into your own project (VB.Net only)*

NOTE: For the methods with the * you will need to also copy the dictionary files to the applications path

Next, simply place this at the very top of your form:

VB.NET
Imports SpellCheck.i00SpellCheck
C#
using i00SpellCheck;

 

Now you will be able to enable control extensions by going:

VB.NET
Me.EnableControlExtensions()
C#
this.EnableControlExtensions(null);

The above line will enable control extensions on all controls that are supported on the form, and all owned forms that are opened.

 

Some other examples are below:

VB.NET
'To load a single control extension on a control call: 
ControlExtensions.LoadSingleControlExtension(TextBox1, New TextBoxPrinter.TextBoxPrinter)

'To enable spell check on single line textboxes you will need to call:
TextBox1.EnableSpellCheck()

'If you wanted to pass in options you can do so by handling the ControlExtensionAdding event PRIOR to calling EnableControlExtensions:
AddHandler ControlExtensions.ControlExtensionAdding, AddressOf ControlExtensionAdding
'Also refer to the commented ControlExtensionAdding Sub in this form for more info

'You can also enable spell checking on an individual Control (if supported):
TextBox1.EnableSpellCheck()

'To disable the spell check on a Control:
TextBox1.DisableSpellCheck()

'To see if the spell check is enabled on a Control:
Dim SpellCheckEnabled = TextBox1.IsSpellCheckEnabled()
'To see if another control extension is loaded (in this case call see if the TextBoxPrinter Extension is loaded on TextBox1):
Dim PrinterExtLoaded = TextBox1.ExtensionCast(Of TextBoxPrinter.TextBoxPrinter)() IsNot Nothing

'To change spelling options on an individual Control:
TextBox1.SpellCheck.Settings.AllowAdditions = True
TextBox1.SpellCheck.Settings.AllowIgnore = True
TextBox1.SpellCheck.Settings.AllowRemovals = True
TextBox1.SpellCheck.Settings.ShowMistakes = True
'etc

'To set control extension options / call methods from control extensions (in this case call Print() from TextBox1):
Dim PrinterExt = TextBox1.ExtensionCast(Of TextBoxPrinter.TextBoxPrinter)()
PrinterExt.Print()

'To show a spellcheck dialog for an individual Control:
Dim iSpellCheckDialog = TryCast(TextBox1.SpellCheck, i00SpellCheck.SpellCheckControlBase.iSpellCheckDialog)
If iSpellCheckDialog IsNot Nothing Then
    iSpellCheckDialog.ShowDialog()
End If

'To load a custom dictionary from a saved file:
Dim Dictionary = New i00SpellCheck.FlatFileDictionary("c:\Custom.dic")

'To create a new blank dictionary and save it as a file
Dim Dictionary = New i00SpellCheck.FlatFileDictionary("c:\Custom.dic", True)
Dictionary.Add("CustomWord1")
Dictionary.Add("CustomWord2")
Dictionary.Add("CustomWord3")
Dictionary.Save()

'To Load a custom dictionary for an individual Control:
TextBox1.SpellCheck.CurrentDictionary = Dictionary

'To Open the dictionary editor for a dictionary associated with a Control:
'NOTE: this should only be done after the dictionary has loaded (Control.SpellCheck.CurrentDictionary.Loading = False)
TextBox1.SpellCheck.CurrentDictionary.ShowUIEditor()

'Repaint all of the controls that use the same dictionary...
TextBox1.SpellCheck.InvalidateAllControlsWithSameDict()




''This is used to setup spell check settings when the spell check extension is loaded:
Private Sub ControlExtensionAdding(ByVal sender As Object, ByVal e As ControlExtensionAddingEventArgs)
    Dim SpellCheckControlBase = TryCast(e.Extension, SpellCheckControlBase)
    If SpellCheckControlBase IsNot Nothing Then
        Static SpellCheckSettings As i00SpellCheck.SpellCheckSettings 'Static for settings to be shared amongst all controls, use dim for control specific settings...
        If SpellCheckSettings Is Nothing Then
            SpellCheckSettings = New i00SpellCheck.SpellCheckSettings
            SpellCheckSettings.AllowAdditions = True 'Specifies if you want to allow the user to add words to the dictionary
            SpellCheckSettings.AllowIgnore = True 'Specifies if you want to allow the user ignore words
            SpellCheckSettings.AllowRemovals = True 'Specifies if you want to allow users to delete words from the dictionary
            SpellCheckSettings.AllowInMenuDefs = True 'Specifies if the in menu definitions should be shown for correctly spelled words
            SpellCheckSettings.AllowChangeTo = True 'Specifies if "Change to..." (to change to a synonym) should be shown in the menu for correctly spelled words
        End If
        SpellCheckControlBase.Settings = SpellCheckSettings
    End If
End Sub
C#
//To load a single control extension on a control call: 
ControlExtensions.LoadSingleControlExtension(TextBox1, New TextBoxPrinter.TextBoxPrinter());

//To enable spell check on single line textboxes you will need to call:
TextBox1.EnableSpellCheck(null);

//If you wanted to pass in options you can do so by handling the ControlExtensionAdding event PRIOR to calling EnableControlExtensions:
ControlExtensions.ControlExtensionAdding += ControlExtensionAdding;
//Also refer to the commented ControlExtensionAdding Sub in this form for more info

//You can also enable spell checking on an individual Control (if supported):
TextBox1.EnableSpellCheck(null);

//To disable the spell check on a Control:
TextBox1.DisableSpellCheck();

//To see if the spell check is enabled on a Control:
bool SpellCheckEnabled = TextBox1.IsSpellCheckEnabled();
//To see if another control extension is loaded (in this case call see if the TextBoxPrinter Extension is loaded on TextBox1):
var PrinterExtLoaded = TextBox1.ExtensionCast<textboxprinter.textboxprinter>() != null;

//To change options on an individual Control:
TextBox1.SpellCheck(true, null).Settings.AllowAdditions = true;
TextBox1.SpellCheck(true, null).Settings.AllowIgnore = true;
TextBox1.SpellCheck(true, null).Settings.AllowRemovals = true;
TextBox1.SpellCheck(true, null).Settings.ShowMistakes = true;
//etc

//To set control extension options / call methods from control extensions (in this case call Print() from TextBox1):
object PrinterExt = TextBox1.ExtensionCast<textboxprinter.textboxprinter>();
PrinterExt.Print();

//To show a spellcheck dialog for an individual Control:
var iSpellCheckDialog = TextBox1.SpellCheck(true,null) as i00SpellCheck.SpellCheckControlBase.iSpellCheckDialog;
if (iSpellCheckDialog != null) {
    iSpellCheckDialog.ShowDialog();
}

//To load a custom dictionary from a saved file:
i00SpellCheck.FlatFileDictionary Dictionary = new i00SpellCheck.FlatFileDictionary("c:\\Custom.dic", false);

//To create a new blank dictionary and save it as a file
i00SpellCheck.FlatFileDictionary Dictionary = new i00SpellCheck.FlatFileDictionary("c:\\Custom.dic", true);
Dictionary.Add("CustomWord1");
Dictionary.Add("CustomWord2");
Dictionary.Add("CustomWord3");
Dictionary.Save(Dictionary.Filename, true);

//To Load a custom dictionary for an individual Control:
TextBox1.SpellCheck(true, null).CurrentDictionary = Dictionary;

//To Open the dictionary editor for a dictionary associated with a Control:
//NOTE: this should only be done after the dictionary has loaded (Control.SpellCheck.CurrentDictionary.Loading = False)
TextBox1.SpellCheck(true, null).CurrentDictionary.ShowUIEditor();

//Repaint all of the controls that use the same dictionary...
TextBox1.SpellCheck(true, null).InvalidateAllControlsWithSameDict(true);




//This is used to setup spell check settings when the spell check extension is loaded:
static i00SpellCheck.SpellCheckSettings SpellCheckSettings = null;//Static for settings to be shared amongst all controls, use "i00SpellCheck.SpellCheckSettings SpellCheckSettings = null;" in the method below for control specific settings...
private void ControlExtensionAdding(object sender, i00SpellCheck.MiscControlExtension.ControlExtensionAddingEventArgs e)
{
    var SpellCheckControlBase = e.Extension as SpellCheckControlBase;
    if (SpellCheckControlBase != null)
    {
        //i00SpellCheck.SpellCheckSettings SpellCheckSettings = null;
        if (SpellCheckSettings == null)
        {
            SpellCheckSettings = new i00SpellCheck.SpellCheckSettings();
            SpellCheckSettings.AllowAdditions = true; //Specifies if you want to allow the user to add words to the dictionary
            SpellCheckSettings.AllowIgnore = true; //Specifies if you want to allow the user ignore words
            SpellCheckSettings.AllowRemovals = true; //Specifies if you want to allow users to delete words from the dictionary
            SpellCheckSettings.AllowInMenuDefs = true; //Specifies if the in menu definitions should be shown for correctly spelled words
            SpellCheckSettings.AllowChangeTo = true; //Specifies if "Change to..." (to change to a synonym) should be shown in the menu for correctly spelled words
        }
        SpellCheckControlBase.Settings = SpellCheckSettings;
    }
}</textboxprinter.textboxprinter></textboxprinter.textboxprinter>

Even more examples are included in the Test project in the download.

Plugins

Since version 20120618 i00SpellCheck has plugin support.

Plugins in i00SpellCheck allow components to be spell checked by making a dll or exe file that contains a public class that inherits i00SpellCheck.SpellCheckControlBase.

They automatically get picked up and allow the spellchecking of extra controls, with no reference to the file itself required. However you will need to place them in the applications path.

The use of plugins allow users to enable spellchecking of their controls, without having to change the i00SpellCheck project.

Another use for them could be to allow the programmer to quickly see if they have spelling errors on forms etc. For example placing the LabelPlugin.exe file in an application path (that already uses i00SpellCheck) will cause all labels in the existing project to be spell checked ... with NO code changes! When the developer wants to deploy their application they simply need to remove the LabelPlugin.exe file, and labels will no longer be corrected.

The basic procedures for creating a plugin are as follows:

  • Start by creating a new project (class library or exe)
  • Reference i00SpellCheck
  • Make a class that inherits i00SpellCheck.SpellCheckControlBase
  • Override the ControlType Property to return the type of control that you want your plugin to spell check
  • Add your code

For examples on inheriting i00SpellCheck.SpellCheckControlBase check out the examples in the Plugins path in the download.

Projects

The included projects and a brief description of each are as follows:

  • i00SpellCheck - Contains the classes for the spellcheck core / TextBox and DataGridView plugins plus the core components required for other plugins to work
  • Plugins\LabelPlugin - Contains a plugin that checks the spelling of Labels
  • Plugins\OSControlRenderer - Contains aplugin that renders the TreeView and ListViews to the same that they appear in Windows
  • Plugins\SelectedControlHighlight - Contains a plugin that extends a variety of Controls so that they appear to have a "glow" when selected
  • Plugins\TextBoxPrinter - Contains a plugin that extends TextBoxBase to support printing
  • Plugins\TextBoxSpeechRecognition - Contains a plugin that extends TextBoxBase to include support for voice input (double tap F12) and dictation
  • Plugins\TextBoxTranslator - Contains a plugin that extends TextBoxBase to support Google Translation
  • Plugins\3rd Party\FastColoredTextBoxPlugin - Contains a plugin that checks the spelling of the FastColoredTextBox control by Pavel Torgashov
  • Tests\BasicTest - A test project that contains basic implementation of i00 SpellCheck
  • Tests\CSharpTest - A test project demonstrating the use of i00 Spell Check in C#
  • *Tests\Test - A test project that contains advanced implementation of i00 SpellCheck and control extensions
  • Tests\3rd Party Dictionaries\HanksDictionaryTest - A test project that demonstrates the use of 3rd party spelling engines in i00 Spell Check. Hank's dictionary in this case (by tewuapple (Hank))
  • Tests\3rd Party Dictionaries\OpenOfficeHunspellDictionaryTest - A test project that demonstrates the use of 3rd party spelling engines in i00 Spell Check. Hunspell in this case which has support for open office dictionaries
  • Tests\3rd Party Dictionaries\WordDictionaryTest - A test project that demonstrates the use of 3rd party spelling engines in i00 Spell Check. Microsoft Word in this case

* = default startup project

Points of Interest

The words that get checked are added to a dictionary cache to speed up checking - the smaller cache is checked first. If the word is not found in the cache, then it checks the main dictionary.

I use fields (public variables) instead of properties for some basic classes, as they are about 2x faster than properties.

Downloads

Total Downloads: Image 13

Downloads per day:
Image 14

Change Log

(Bold items are things that end users will notice)

20140111

i00SpellCheck
  • Fixed single line textbox issue where EnableSpellCheck would be required to be needed 2x (found by hoodch)
  • Updated the plugin manager to the latest ... now supports paths and shadow copying (this is not implemented in this project)
  • Added help button to spell check dialog (for people who don't know about holding F1)
Test
  • Made enabled button work for each tab on demo form
TextBoxSpeechRecognition
  • Fixed a few issues with the dictation where it would not put in the dictated text
  • Enter now commits dictated text
KonamiCode WebpageImplementation
  • Started implementation for a web interface (currently at a very basic stage)

20130521

i00SpellCheck
  • Fixed bug that would occur when multiple textboxbases shared a context menu (found by Adviser)
  • DataGridView CellStyle no longer has to be set to WrapMode.True for corrections to appear in the DataGridView
  • Fixed a possible rendering issue where the error underlines would not be drawn in some instances
  • Fixed a possible bug where, on some rare occasions, the spell check dialog would error upon opening (found by TheComputerMan08 (Brent))
  • Fixed an issue where words starting with a capital were not getting picked up by the spell check
  • Fixed a bug where if selecting a new dictionary with the default ShowUIEditor function the control would not clear old correct words from cache
  • Now automatically spell checks controls in SplitContainers (found by cognositivo)
FastColoredTextBoxPlugin
  • Added HTML example to FCTB with html color highlighting
  • Updated FCTB to latest version
  • Fixed an issue with FCTB where when inserting text all of the text would be spellchecked instead of just the visible range, speeding up large copy and pastes
HanksDictionaryTest
  • Added another example of using a different dictionary with i00 Spell Check ... Hanks Dictionary (by tewuapple (Hank))
WordDictionaryTest
  • Added another example of using a different dictionary with i00 Spell Check ... Word Dictionary

20130114

i00SpellCheck
  • Added engine to make more generic control extensions
  • Changed the workings of SpellCheckControlBase to use the more generic control extensions
  • Default dictionary load is now threadded even when just calling .SpellCheck
  • Control extensions can now specify multiple ControlTypes
  • Put the TextBoxBase change case feature into its own control extension
  • Put the nicer TextBoxBase context menu into its own control extension
  • Made the window animations smoother and more stable for F7 etc
  • Control extensions can now be dependant on other control extensions (like references but within control extensions)
TextBoxPrinter
  • Added TextBoxPrinter plugin
TextBoxSpeechRecognition
  • Added buttons to trigger dictate and speech
  • Custom Karaoke rendering control added to test form
  • Now uses the new, more generic, control extension rather than extending SpellCheckTextBox
  • Speech is no longer "broken up" at the end of each line in Windows 8
OSControlRenderer
  • Added OSControlRenderer plugin
SelectedControlHighlight
  • Added SelectedControlHighlight plugin
TextBoxTranslator
  • Added TextBoxTranslator plugin
Test
  • Neatened up Draft Plan rendering

20121102

i00SpellCheck
  • Made SpellCheckDialog more universal (so it can be used with other controls more easily)
  • Fixed a bug when using the SpellCheckDialog to spell check where the textbox would flicker and repaint several times upon confirming the changes
  • Added IgnoreWordsInUpperCase setting (requested by TheMperor)
  • Added IgnoreWordsWithNumbers setting (requested by TheMperor)
  • Fixed a bug that would cause the balloon tooltip to stuffup if it was on a screen to the left of the primary screen, this could cause the spell check dialog to crash
Test
  • Added some options to the Performance Monitor window
OpenOfficeHunspellDictionary
  • Hunspell now has case error underlineing
  • Added Hunspell syninoum lookup to Hunspell test project
TextBoxSpeechRecognition
  • Tray icon appears when using speech
  • Added a karaoke style content menu item when speaking
  • Now when triggering speek or dictate all instances of speech (across all applications that use i00 spell check) are terminated so that you can't get "multiple people" talking at once

20120920

  • Added performance counter
  • Fixed a bug with the built in dictionary where the dictionary index would be filled up with user words and potentially cause errors, this also has speed up spellchecking a lot
  • Fixed an issue with the spell check dialog, if you had alot of data it would "freeze"

20120914

  • Added Redo to context menu for rich text boxes
  • Changed the way the items are added to the spell check text box content menus to make them more expandable
  • TextBoxSpeechRecognition now adds menu items to TextBoxBase context menus
  • Added properties to TextBoxSpeechRecognition to adjust various settings
  • Fixed a bug that would cause an error when getting sugguestions for a word, where no sugguestsions could be made

20120907

  • Removed some redundant stuff from the project
  • Added a button to the test form that brings up the dictionary editor
  • Changed the way the flat files dictionary index is stored
  • Changed the way the flat files dictionary is stored in memory
  • Changed the dictionary alot to allow for easy user dictionary creation for inherreted dictionary classes
  • Added a C# test project
  • Added a test project to demonstrate how people can use other spelling engines in i00 Spell Check. Hunspell in this case which has support for open office dictionaries

20120903

  • Added SpellCheckControlAdding event that allows you to pass back e.Cancel = True to prevent the control from being checked
  • Fixed a bug that could produce an error if you call .EnableSpellCheck multiple times on several forms (found by rfreedlund)
  • Indexed dictionary - added slightly to the initial loading time... but sped up checking significantly (requested by Maverickz)
  • Fixed a rare occuring bug where an error would be thrown with the spellcheck cache
  • User dictionary file is now separate from the built in dictionary
  • Ignored words are now stored in the user dictionary
  • Updated the FlatFile dictionary editor to support the new user dictionary
  • Added i00Binding List to the project for the FlatFile Dictionary editor... you can remove the reference, if you want, but will first have to remove the dictionary editor if you don't require it (found in i00SpellCheck\Spell Check\Engine\Dictionary\Flat File\Editor)
  • Changed the dictionary dramatically to support custom classes to enable the checking of other dictionary formats
  • Added a plugin that extends the SpellCheckTextbox, adding voice recognition to it! However Microsoft's inbuilt speech recognition isn't great. Press F12 twice quickly to perform speech recognition
  • Made built in plugins more extendable

20120625

  • Changed the way the words get added to the dictionary cache (increased spell checking speed)
  • Improved the speed of the FastColoredTextBox Plugin
  • Made the test project automatically pickup any plugins in the project path and add them to tabs automatically - the references to the plugins are not required, they were added for the LabelPlugin and FastColoredTextBoxPlugin so that they automatically get placed in the same folder when the project is built!

20120622 - FastColoredTextBox

  • Changed some internal workings of the spell checker
  • Added support for FastColoredTextBox with included plugin!

20120618 - Plugins!

  • Added test plugin to project that grants the ability to spell check labels
  • Changed the structure / inner workings of the spell check alot / made the spell check more modular and plugins possible!
  • Fixed a bug where the settings were not being applied under certain circumstances to spell check controls
  • Grid view spell checking now is shown on cells even when their not being edited

20120609 - DataGridView's are go!

  • Added support for DataGridViews (requested by in2tech)
  • Fixed a bug where the underlines would not draw in the correct positions when word wrapping is disabled
  • Fixed a bug where single line TextBoxes would not always show spelling errors

20120608 - Disabling and bug fixes

  • Fixed a bug where if you had an apostrophe at the start of a word it would push the underline spacing out (found by jwinney)
  • Added ability to disable the spell check on a TextBox (requested by Gabriel X)
  • Fixed a bug where the standard TextBox was not refreshing properly since the new rendering method was added
  • Fixed bug where if a TextBox contained errors and all text was deleted the underlines would still show (found by jim400)
  • Some minor interface changes with splash screen and "alt" for menu

20120203

  • Tooltips now have image support and images for some definitions
  • Fixed tool tip rendering issues
  • You can now press F3 to change case of the selected text (requested by rykk)
  • Made "-" be classified as a word break char
  • Fixed an error that would re-paint the textbox errors 2x
  • Disabled Cut/Copy/Paste options in menu if not on an STA thread as this would error

20120102 - Happy New Year!

  • Modified definitions to lookup from file dynamically rather than being loaded into memory to reduce RAM usage ~56MB saved!
  • Changed settings so that all the spellcheck settings are in a single class
  • Cleaned up the SpellCheckTextBox class and subclasses to make settings easily editable with a property grid
  • Added property grid to the test project
  • Added a dictionary editor
  • Added a "bare-bones" test project to the solution, to make it simpler for users to see how easy it can be to use i00 .NET Spell Check in your projects!
  • Changed the render method to eliminate redraw flicker, added a setting to revert to the old render method "RenderCompatibility"

20111202 - Now with dialog!

  • Cleaned up some stuff ... moved HTML formatted tooltip + HTML ToolStripItem into their own controls
  • Made the Text ToolStripSeperator look and function better
  • Made the right click menu items portable so that they can be added to any menu for other things - not so tightly bound to the text box
  • Implementing a spell check dialog for F7 style spell checking (select text box and press F7!... can also be called with: TextBoxName.SpellCheck.ShowDialog())

20111109 - In-menu definitions, synonyms and fixes!

  • Changed tooltip definitions to match more words from their word base, e.g. "suggestions" matches "suggestion" for definition since no definition is matched explicitly for suggestions and states that it is plural in the tip
  • Tooltip for definitions is now owner draw so that it appears a little nicer
  • Fixed a case matching bug where "This" would suggest "this" rather than "This" (requested by TxDeadhead)
  • Words like "Chris's" now suggest "Chris'" instead of "Chris's"
  • Words that end in an ' no longer appear as being misspelled
  • Made the context menu position itself a little better if near the bottom or right sides of a screen
  • Fixed a bug where, if you press the context menu button on the keyboard multiple times, the menu would add multiple of the same corrections to the context menu
  • Sped up loading of dictionary file
  • Fixed a bug in the definition file - all adjectives and adverbs were mixed up (i.e., all adjectives were listed as adverbs, and all adverbs were listed as adjectives)!
  • Various speed optimizations in finding word suggestions, to lookup misspelled word "suggestions" used to take ~250ms, now down to ~150ms
  • Improved suggestion lookup now adds higher weight to words with extra duplicates or missing duplicates (such as "running", "running" > "running")
  • Added in-context-menu definitions for correctly spelled words (requested by Dean)
  • Words with interesting cases (such as SUpport, SupporT etc) now get picked up (requested by TxDeadhead)
  • Now doesn't fall over if the dictionary, definitions or synonyms files have been removed - just removes functionality for that bit
  • Added synonyms; "Change to..." menu item (requested by NtEditor)

20111106 - Been busy!

  • Various speed optimizations
  • Dictionary is now stored as a Flat File for portability
  • Added owner draw support for spelling errors
  • Added the ability to customize colors for highlighting misspelled words
  • Added some examples of how to customize the appearance of the spell check
  • Word definitions added for spelling suggestions ... so if you are unsure of the correct spelling out of the suggestions, you can pick the correct one from the definition
  • The right click menu in .NET comes up from the middle of a text box when pressing the context menu button on the keyboard - It has now been modified to pop-out from the carets location
  • Cross word generator - plan to make solver later too!
  • Support added for Rich Text Boxes
  • Added Suggestion Lookup Example

20111011 - Some fun extras

  • Added anagram lookup
  • Added Scrabble helper

20111008 - Minor changes

  • Fixed a bug where the text box underlines would not always draw initially until the textbox was scrolled or had some text changed
  • Cleaned up the interface to made it look more professional

20111006

  • Initial Release

Possible Issues

SpellCheckTextBox

Since the Textbox has no way to really draw on it "nicely", I used to capture the WM_PAINT of the control and then draw on the textbox graphics that was set by going Graphics.FromHwnd... this seemed to work well but produced a slight flicker that I thought was undesirable...

As of version 20120102, the render method now uses layered windows (by default), this basically means that all of the underlines that appear to be drawn on the control are actually drawn on another window over the top of the control ...

So how does this affect the user? Well in most cases it doesn't, the form is click-through and only the drawings are visible not the form itself. In fact if you press start + tab in Windows Vista / 7 to activate Flip3D, it even appears on the same window!

As I said above "in most cases"...

MIDI forms I haven't tested, but am quite sure that they won't work using the new render method.

With overlapping textbooks the text corrections on the underneath control "float"over the control that is on top, and if the textbox is off the form, the corrections "float" off the form!

So in cases such as the above, you will have to go back to the older "compatible" rending, this can be done in these cases by going:

C#
DirectCast(TextBox.SpellCheck, SpellCheckTextBox).RenderCompatibility = True

Thanks

A special thanks to Pavel Torgashov for his excellent FastColoredTextBox control. This control is used in the solution to test i00SpellCheck's plugin architecture with 3rd party controls. i00 has not modified this control in any way and is only responsible for integrating the spell checking ability to it via an i00SpellCheck plugin. In no way is this control required for spell checking functions in other projects within the solution.

Thanks for downloading.

Suggestions on possible improvements are much appreciated.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
i00
Software Developer (Senior) i00 Productions
Australia Australia
I hope you enjoy my code. It's yours to use for free, but if you do wish to say thank you then a donation is always appreciated.
You can donate here.

Comments and Discussions

 
AnswerRe: No easy way to bring *.vb files directly into project Pin
Robert Gustafson29-Nov-19 19:59
Robert Gustafson29-Nov-19 19:59 
GeneralRe: No easy way to bring *.vb files directly into project Pin
i0010-Feb-20 15:14
i0010-Feb-20 15:14 
QuestionI am using i00 spell check in my project Pin
Member 1461003829-Oct-19 15:58
Member 1461003829-Oct-19 15:58 
QuestionI am using i00 spell check in my project Pin
Tom00729-Oct-19 12:20
Tom00729-Oct-19 12:20 
QuestionConsideration when using this tool inside custom controls Pin
Robert Gustafson26-Oct-19 19:21
Robert Gustafson26-Oct-19 19:21 
AnswerRe: Consideration when using this tool inside custom controls Pin
Robert Gustafson18-Nov-19 10:57
Robert Gustafson18-Nov-19 10:57 
QuestionI am using i00 Spell Check in my project Pin
Robert Gustafson23-Oct-19 1:20
Robert Gustafson23-Oct-19 1:20 
QuestionSome issues with text-changing and RECOMMENDED CHANGES Pin
Robert Gustafson17-Oct-19 18:05
Robert Gustafson17-Oct-19 18:05 
RECOMMENDED CHANGES:

Here are some modifications that I'm recommending for the i00SpellCheck project inside the SpellCheck solution:

First, not a change to the project itself, but a useful tip: The spell-checker checks the entire text box or rich text box. There's no way to restrict the spell-check to a highlighted (selected) region. To do so, you need to create a second text-box/rich-text-box--say, one unattached to any form--transfer the selected text of the first text box into it (using SelectedText/SelectedRtf of the first text-box/rich-text-box and Text/Rtf of the second), spell-check on the second one, then paste the result back into the first text-box. For instance:
VB
Dim rtb As RichTextBox = New RichTextBox()
rtb.SelectedRtf = RichTextBox1.SelectedRtf
Dim iSpellCheckDialog = _
    TryCast(rtb.SpellCheck, i00SpellCheck.SpellCheckControlBase.iSpellCheckDialog)
If iSpellCheckDialog IsNot Nothing Then
   iSpellCheckDialog.ShowDialog()
    RichTextBox1.SelectionProtected = False 'guarantees paste
    rtb.SelectAll() : RichTextBox1.SelectedRtf = rtb.SelectedRtf '(prevent line break)
End If


Now for the changes:

1. With selection "Change All" in the spell-check dialog, all subsequent unrecognized words are replaced, wily-nilly, with the first item in each word's suggestion list, even if arbitrary replacement text or another list entry is specified in "Change To:", even if only all occurrences of 1 word is desired for change. To make it so that subsequent occurrences only of the current misspelled word are replaced, and with the contents of the txtChangeTo text (if it isn't empty), I suggest making the following changes to the following changes to SpellCheckDialog.vb:
VB
Dim NewTextForChangeAll, OldTextForChangeAll As String 'for ChangeAll
    Private Sub StartChangeAll()
        btnAdd.Enabled = False
        ChangeingAll = True
        If mt_ChangeAll IsNot Nothing AndAlso mt_ChangeAll.IsAlive Then
            mt_ChangeAll.Abort()
        End If
        Dim SelectedWord = Me.SelectedWord
       NewTextForChangeAll = txtChangeTo.Text
	OldTextForChangeAll = SelectedWord.OrigWord 
       If SelectedWord IsNot Nothing Then
            SelectedWord.Selected = False
        End If
        ShowHideSuggestions(False)

        btnChangeAll.Enabled = False
        mt_ChangeAll = New System.Threading.Thread(AddressOf ChangeAll)
        mt_ChangeAll.Name = "Spell Check - Change all"
        mt_ChangeAll.IsBackground = True
        mt_ChangeAll.Start()
    End Sub

    Private Sub ChangeAll()

        'Dim st = Now

        Dim WordErrors = (From xItem In HtmlSpellCheck1.Words Where (xItem.SpellCheckState = HTMLSpellCheck.SpellCheckDialogWords.SpellCheckStates.Case OrElse xItem.SpellCheckState = HTMLSpellCheck.SpellCheckDialogWords.SpellCheckStates.Error))
        For Each item In WordErrors.ToArray
        If String.Compare(item.NewWord, OldTextForChangeAll, True) = 0 Then
					'   word in question found
					Dim Suggestions = GetSuggestions(item.NewWord)
					If Suggestions.Count > 0 Then
						If String.IsNullOrEmpty(NewTextForChangeAll) Then
							'change to first if no text offered
							item.NewWord = Suggestions.First
						 Else
							'   change to offered text
							item.NewWord = NewTextForChangeAll
						End If
						 item.SpellCheckState = HTMLSpellCheck.SpellCheckDialogWords.SpellCheckStates.OK
						 If SelectedWord() Is item Then
							  HtmlSpellCheck1_SelectionChanged(HtmlSpellCheck1, New HTMLSpellCheck.HTMLWordEventArgs() With {.Word = item})
						 End If
					End If
				End If
        Next
        btnRevertAll.SafeInvoke(Function(x As Button) InlineAssignHelper(x.Enabled, True))

        'complete... unless...
        If HtmlSpellCheck1.mt_SpellCheck IsNot Nothing AndAlso HtmlSpellCheck1.mt_SpellCheck.IsAlive Then
            '... we are still spell checking :(
        Else
            Dim btnChangeAllEnabled = (From xItem In HtmlSpellCheck1.Words Where (xItem.SpellCheckState = HTMLSpellCheck.SpellCheckDialogWords.SpellCheckStates.Case OrElse xItem.SpellCheckState = HTMLSpellCheck.SpellCheckDialogWords.SpellCheckStates.Error)).Count > 0
            ChangeingAll = False
            If btnChangeAllEnabled = True Then
                '...or we have words left that suggestions could not be found for...
                btnChangeAll.SafeInvoke(Function(x As Button) InlineAssignHelper(x.Enabled, btnChangeAllEnabled))
                btnSkip_Click(btnSkip, EventArgs.Empty)
            Else
                CompleteSpellCheck()
            End If
        End If

        'MsgBox(Now.Subtract(st).TotalMilliseconds)

    End Sub


2. To allow the programmer to keep the standard menu from being automatically added to a text-box's/rich-text-box's context menu, add the following to Extension.vb:
VB
	 Friend IncludeStandardItems As Boolean = True
	 <System.Runtime.CompilerServices.Extension()> _
	 Public Sub IncludeOrExcludeStandardMenu(ByVal sender As Control, _
		Optional ByVal Include As Boolean = True)
	 IncludeStandardItems = Include
	 End Sub

Then change extTextBoxContextMenu.vb as follows:
VB
Public Sub AddStandardItems(ByVal ContextMenuStrip As ContextMenuStrip)
        If Not IncludeStandardItems Then
            Exit Sub 'suppress standard menu
        End If
    RaiseEvent PreAddingMenuItems(Me, EventArgs.Empty)

    If ContextMenuStrip.Items.Count > 0 Then
        ContextMenuStrip.Items.Add(New StandardToolStripSeparator)
    End If

    Undo = New StandardToolStripMenuItem("&Undo", My.Resources.Undo)
    ContextMenuStrip.Items.Add(Undo)

    Dim RichTextBox = TryCast(TextBox, RichTextBox)
    If RichTextBox IsNot Nothing Then
        Redo = New StandardToolStripMenuItem("&Redo", My.Resources.Redo)
        ContextMenuStrip.Items.Add(Redo)
        Redo.Enabled = RichTextBox.CanRedo
    End If

    ContextMenuStrip.Items.Add(New StandardToolStripSeparator)

    If System.Threading.Thread.CurrentThread.GetApartmentState = Threading.ApartmentState.STA Then
        'allow cut/copy/paste - doesn't work unless STA thread
        Cut = New StandardToolStripMenuItem("Cu&t", My.Resources.Cut)
        ContextMenuStrip.Items.Add(Cut)
        Copy = New StandardToolStripMenuItem("&Copy", My.Resources.Copy)
        ContextMenuStrip.Items.Add(Copy)
        Paste = New StandardToolStripMenuItem("&Paste", My.Resources.Paste)
        ContextMenuStrip.Items.Add(Paste)

        Cut.Enabled = TextBox.SelectionLength > 0
        Copy.Enabled = TextBox.SelectionLength > 0
        Paste.Enabled = Clipboard.GetText <> ""
    End If

    Delete = New StandardToolStripMenuItem("&Delete", My.Resources.Delete)
    ContextMenuStrip.Items.Add(Delete)

    ContextMenuStrip.Items.Add(New StandardToolStripSeparator)

    SelectAll = New StandardToolStripMenuItem("Select &All", My.Resources.SelectAll)
    ContextMenuStrip.Items.Add(SelectAll)

    'enable / disable

    Undo.Enabled = TextBox.CanUndo

    Delete.Enabled = TextBox.SelectionLength > 0

    SelectAll.Enabled = TextBox.SelectionLength <> Len(TextBox.Text)

    RaiseEvent PostAddingMenuItems(Me, EventArgs.Empty)

End Sub

Now, calling Me.IncludeOrExcludeStandardMenu(False) before Me.EnableControlExtensions() will suppress the auto-addition of the standard menu. (The default parameter value for the new procedure is True to allow auto-addition.)

3. The spell check won't recognize valid words which are broken by hidden (syllable) hyphens (ChrW(173)). To rectify that, I would make the following changes:

First, add the following to iDictionary.vb:
VB

Public Shared Function RemoveHyphens(ByVal Word As String) As String
'   strip out hidden (syllable) hyphens
Return Word.Replace(ChrW(173), "")
End Function

Then add the following statement
VB
Word = Dictionary.RemoveHyphens(Word) 'ignore hidden (syllable) hyphens

in the following places:

a. in front of the 1st statement of the following procedures: SpellCheckWord and SpellCheckSuggestions (both in iDictionary.vb); AddItems (in Menu.vb); DictionaryIgnoreWord, DictionaryUnIgnoreWord, DictionaryAddWord, and DictionaryRemoveWord (all in SpellCheckControlBase.vb); and, finally,

b. in the following place in SpellCheckDialog.vb:
VB
Private Sub btnAdd_ClickDropdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.ClickDropdown
Dim theSelectedWord = SelectedWord()
If theSelectedWord IsNot Nothing Then
    Dim Word As String = txtChangeTo.Text
    If ChangeToChanged = False Then
        Word = theSelectedWord.NewWord
    End If
Word = Dictionary.RemoveHyphens(Word) 'ignore hidden (syllable) hyphens
    tsiAddCaseSensitive.Text = "Case sensitive: " & Word
    tsiAddCaseInsensitive.Text = "Case insensitive: " & LCase(Word)
    tsiAddCaseSensitive.Visible = Word <> LCase(Word)
    cmsAdd.Show(btnAdd, New Point(0, btnAdd.Height))
End If
End Sub


4. Also, you might want to set StartPosition for the SpellCheckDialog form to CenterScreen.

5. I also recommend the following changes in order to ensure that i00SpellCheck's recognition of numbers and works breaks is more similar to (although not identical to) Microsoft Word's. First make the following changes in the "Word Breaks" and "For words with 's" regions of Formatting.vb--changing RemoveWordBreaks and RemoveApoS as follows (and removing the WordBreakChrs array and associated "escaping" procedure):
VB
#Region "Word Breaks"
   
    '   Word-break characters include the following characters:
    '      1. Straight/left/right single quote chars (when not both preceded
    '         by an alphanumeric char [letter/digit] and followed by a letter)
    '      2. Ampersand char (when neither preceded nor followed by alphanumeric
    '         or currency char)
    '      3. Numeral-sign, trademark, and service-mark chars (when not both
    '         preceded and followed by alphanumeric chars)
    '      4. Currency, percent, and per-mille chars (when not preceded nor
    '         followed by other such chars or alphanumeric chars)
    '      5. Copyright, registration, and publishing chars (when not preceded
    '         by an alphanumeric char)
    '      6. White-spaces, carriage returns, line feeds, horizontal & vertical tabs,
    '         and form feeds/hard page breaks (always)
    '      7. Plus and minus signs (when not both preceded by decimal number and then
    '         "E"/"e" AND followed by integer [allows screening for exponential-format
    '         numbers])
    '      8. Any OTHER punctuation and math chars except optional/soft (syllable)
    '         hyphens (always)
    '   NOTE:
    '      "Syllable" (soft) hyphens--which are optional word "infixes" (that should
    '      be ignored by the spell-checker engine)--are never counted as word breaks;
    '      "required" (hard) hyphens and en/em dashes are always counted as breaks

    '   Regular-expression constants
    Private Const FastOption As RegexOptions = _
       RegexOptions.Compiled Or RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase
    Private Const HyphenChar As String = ChrW(173), _
       SingleQuoteChars As String = "'`" & ChrW(1370) & ChrW(1371) & ChrW(1372)  _
          & ChrW(8216) & ChrW(8217) & ChrW(8219) & ChrW(8242) _
          & ChrW(11788) & ChrW(11789) & ChrW(65040) & ChrW(65287), _
       SingleQuoteTest As String = "[" & SingleQuoteChars & "]", _
       AmpersandChars As String = "&﹠&&", _
       AmpersandTest As String = "[" & AmpersandChars & "]", _
       NumberSignChars As String = "#﹟#♯", _
       NumberSignTest As String = "[" & NumberSignChars & "]", _
       PercentPermilleChars As String = "%‰﹪‱%٪؉؊", _
       PercentPermilleTest As String = "[" & PercentPermilleChars & "]"
    Private Const ApoSTest As String = SingleQuoteTest & "s(?![\p{L}\d])"
    Private Const WordBreakCharTest As String = "(?<![\p{L}\d])'|'(?!\p{L})" _
       & "|(?<![\p{L}\d\p{Sc}])&(?![\p{L}\d\p{Sc}])" _
       & "|(?<![\p{L}\d])[#™℠]|[#™℠](?![\p{L}\d])" _
       & "|(?<![\p{Sc}%\p{L}\d])[\p{Sc}%](?![\p{Sc}%\p{L}\d])" _
       & "|(?<![\p{L}\d])[©®℗]|[\s\r\n\t\v\f]" _
       & "|(?<!(^|[^\p{L}\d])\d+E)[+\-]|[+\-](?!\d+($|[^\p{L}\d]))" _
       & "|[\p{P}\p{Sm}\^~≈-[" & HyphenChar & "'&#™℠\p{Sc}%©®℗+\-]]"
    '   Reular-expression instances
    Private Shared ReadOnly reReplaceWordBreakChars As Regex = _
          New Regex(WordBreakCharTest, FastOption), _
       reReplaceSingleQuotes As Regex = New Regex(SingleQuoteTest, FastOption), _
       reReplaceAmpersands As Regex = New Regex(AmpersandTest, FastOption), _
       reReplaceNumberSigns As Regex = New Regex(NumberSignTest, FastOption), _
       reReplacePercentPermille As Regex = New Regex(PercentPermilleTest, FastOption), _
       reRemoveTrailingApoS As Regex = New Regex(ApoSTest, FastOption)  

    Public Shared Function RemoveWordBreaks(ByVal Text As String) As String
       '   Convert characters in the following order:
       '   1. Left/right single-quotes of all kinds to straight single quotes
       '   2. Ampersands of all kinds to regular ampersands
       '   3. Number-signs of all kinds to regular number-signs
       '   4. Percent/per-mille chars of all kinds to regular percent chars
       '   5. All valid word-break chars to spaces (subject to conditons above)
       If Text <> "" Then
          Text = reReplaceSingleQuotes.Replace(Text, "'")
          Text = reReplaceAmpersands.Replace(Text, "&")
          Text = reReplaceNumberSigns.Replace(Text, "#")
          Text = reReplacePercentPermille.Replace(Text, "%")
          Text = reReplaceWordBreakChars.Replace(Text,  " ")
       End If
       Return Text
    End Function

#End Region

#Region "For words with 's"
   
    Friend Shared Function RemoveApoS(ByVal text As String) As String
       '   Remove "'s" at the ends of words (apostrophy can be straight, left, or right)
       '   (NOTE: This does not remove "'s" if "s" is followed by an alphanumeric char)
       Return _
          reRemoveTrailingApoS.Replace(text, "")
    End Function

#End Region

Then change the code in the SpellCheckWordNonUser procedure of FlatFileSpellCheck.vb as follows:
VB
#Region "Check that Word is in the Dictionary"
    '   Regular-expression constants and instances to verify that numeric
   '   text is either by itself, with single currency symbol at beginning
   '   and/or end (but not "infixed" within), or with single percent/per-mille
   '   symbol at end (but not anywhere else)--and to then remove such characters
   Private Const FastOption As RegexOptions = _
      RegexOptions.Compiled Or RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase
   Private Const PercentPermilleChars As String = "%‰﹪‱%٪؉؊", _
      PercentPermilleTest As String = "[" & PercentPermilleChars & "]", _
      SpecialChars As String = "\p{Sc}" & PercentPermilleChars, _
      SpecialCharsTest As String = "[" & SpecialChars & "]", _
      NumericText As String = "[^" & SpecialChars & "]+", _
      ValidNumberTest As String = "^(" & NumericText & "|\p{Sc}?" & NumericText & "\p{Sc}?" _
         & "|" & NumericText & PercentPermilleTest & "?)$"
   Private ReadOnly reCheckValidNumber As Regex = New Regex(ValidNumberTest, FastOption), _
      reRemoveSpecialChars As Regex = New Regex(SpecialCharsTest, FastOption)      

   Public Overrides Function SpellCheckWordNonUser(ByVal Word As String) _
         As Dictionary.SpellCheckWordError
      '   blank word?
      If Word = "" Then
         Return SpellCheckWordError.OK
      End If
      '   Doing this directly on the word object did not work????:
      Dim theWord = Word
      '   Strip "'s"
      Dim OldWord = theWord
      theWord = Dictionary.Formatting.RemoveApoS(theWord)
      '   ignore numbers
      Dim NumericWord = theWord
      '   valid number syntax with respect to currency/percent/per-mille chars
      If reCheckValidNumber.IsMatch(NumericWord) Then
         '   further validate number with special chars removed
         NumericWord = reRemoveSpecialChars.Replace(NumericWord, "")
         If IsNumeric(NumericWord) Then
            Return SpellCheckWordError.OK
         End If
      End If
      '   add words that start with that letter only
      Dim DicWords = IndexedDictionary.Item(Word)
      If DicWords Is Nothing Then
         '   '   allow word in caps...
         '   If Formatting.AllInCaps(Word) Then
         '      Return SpellCheckWordError.OK
         '   End If
         Return SpellCheckWordError.SpellError
      End If
      DicWords = (From xItem In DicWords Where LCase(xItem) = LCase(theWord)).ToList
      If DicWords.Count = 0 Then
        Return SpellCheckWordError.SpellError 'no version of word found
      End If
      For Each iDicWord In DicWords
         '   words found
         Dim WordCaseOK = Formatting.CaseOK(Word, iDicWord)
         If WordCaseOK Then
            Return SpellCheckWordError.OK 'found version with acceptable case
         End If
      Next
      '   case error
      Return SpellCheckWordError.CaseError
   End Function

#End Region


6.There are also issues with RichTextBox's which contain hidden text (text marked as "invisible" using RTF code "\v text\v0") whenever the platform of the host project of this DLL is .NET Framework 4.7 or higher: The Text, SelectedText, and SelectionLength properties return different values than they do for earlier .NET platforms, ignoring the invisible text. (There are also issues with proper function of methods that convert between point-on-control and character-index when the end of a document isn't padded with enough "visible" but undisplayed characters--like optional "syllable" hyphens"--to compensate for the earlier marked-as-invisible text. Weird!) In order to ensure that the spell-check dialog properly locates and (when asked) replaces misspelled text in a rich-text box containing invisible text, regardless of target platform, do the following:

Add the following extension methods to the Extension.vb file:
VB

#Region "Extended Text Reading"
 	''' <summary>
	''' Gets full length of selected text, including any hidden text
	''' </summary>
	''' <param name="RichTextBox"></param>
	''' <returns>Full length of selection</returns>
	''' <remarks>This is different from the SelectionLength property only when
	''' the host program is targeted for .NET 4.7 or later, TextBox is a RichTextBox,
   ''' and hidden text is present in document</remarks>
	<System.Runtime.CompilerServices.Extension()> _
	Public Function FullSelectionLength(TextBox As TextBoxBase) As Integer
   If TypeOf TextBox IsNot RichTextBox Then
      '   standard text box
      Return TextBox.SelectionLength
   End If
	'   rich text box--get start and end of selection
   Dim RichTextBox As RichTextBox = DirectCast(TextBox, RichTextBox)
	Dim TypeRTB As Type = RichTextBox.GetType
	Dim fi As FieldInfo = _
		TypeRTB.GetField("curSelStart", _
			BindingFlags.Instance Or BindingFlags.NonPublic)
	Dim nCurSelStart As Integer = CType(fi.GetValue(RichTextBox), Integer)
	fi = TypeRTB.GetField("curSelEnd", _
		BindingFlags.Instance Or BindingFlags.NonPublic)
	Dim nCurSelEnd As Integer = _
		Math.Min(CType(fi.GetValue(RichTextBox), Integer), RichTextBox.TextLength)
	If nCurSelStart = -1 AndAlso nCurSelEnd = -1 Then
		Return 0
	 Else	
		Return _
			nCurSelEnd - nCurSelStart
	End If
	End Function

	''' <summary>
	''' Gets full plain text of rich-text box (or selection thereof),
	''' including any hidden text
	''' </summary>
	''' <param name="SelectionOnly">True to get only selected text,
	''' False (default) to get entire text</param>
	''' <returns>Full plain text, be it entire document or selection</returns>
	''' <remarks>This is different from the Text or SelectedText property only when
	''' the host program is targeted for .NET 4.7 or later, TextBox is a RichTextBox,
   ''' and hidden text is present in document</remarks>
	<System.Runtime.CompilerServices.Extension()> _
	Public Function ExtendedText(TextBox As TextBoxBase, _
		Optional SelectionOnly As Boolean = False) As String
   If TypeOf TextBox IsNot RichTextBox Then
      '   standard text box
      If SelectionOnly Then
         Return TextBox.SelectedText
       Else
         Return TextBox.Text
      End If
   End If
	'   rich text box--get plain text including any hidden text
   Dim RichTextBox As RichTextBox = DirectCast(TextBox, RichTextBox)
	Dim TypeRTB As Type = RichTextBox.GetType
	'   access private WindowText property
	Dim prop As PropertyInfo = _
		TypeRTB.GetProperty("WindowText", _
			BindingFlags.Instance Or BindingFlags.NonPublic)
	Dim Text As String = _
		CType(prop.GetValue(RichTextBox, {}), String).Replace(ControlChars.Cr, "")
	If SelectionOnly Then
		'   get start and end of selection
		Dim fi As FieldInfo = _
			TypeRTB.GetField("curSelStart", _
				BindingFlags.Instance Or BindingFlags.NonPublic)
		Dim nCurSelStart As Integer = CType(fi.GetValue(RichTextBox), Integer)
		fi = TypeRTB.GetField("curSelEnd", _
			BindingFlags.Instance Or BindingFlags.NonPublic)
		Dim nCurSelEnd As Integer = _
			Math.Min(CType(fi.GetValue(RichTextBox), Integer), Text.Length)
		'   extract substring
		If (nCurSelStart = -1 AndAlso nCurSelEnd = -1) _
				OrElse nCurSelStart = nCurSelEnd Then
			Text = "" 'no selection
		 Else
			Text = Text.Substring(nCurSelStart, nCurSelEnd - nCurSelStart)
		End If
	End If
	Return Text
	End Function
#End Region

Then, in every other ".vb" code file in the "i00SpellCheck" project, replace all "reading" occurrences (those inside an expression) of Text, SelectedText, and SelectionLength properties that are attached to TextBoxBase-type variables--specifically, to TextBox and parentTextBox--with ExtendedText (aka. all text), ExtendedText(True) (aka. selected text), and FullSelectionLength, respectively. Do not replace occurrences of these properties that are "writing" (on the left-hand-side of an assignment statement) or attached to variables of other objects--particularly those that don't derive from TextBoxBase! These changes should affect the following files: clsRTBHighlight.vb, Drawing.vb, extTextBoxChangeCase.vb, extTextBoxCommon.vb, extTextBoxContextMenu.vb, Menu.vb, and Misc.vb.

(Note that these fixes don't necessarily guarantee that spell-check via squiggly lines and a context menu for a misspelled word will display and function correctly when there's invisible text. More changes will be needed to finesse that.)

7. You might also want to make the following change to SetUpControl within TextBox.vb, in order to ensure that i00SpellCheck words with controls that derive from TextBox and RichTextBox:
VB
Region "Test Harness"

    Public Function SetupControl(ByVal Control As System.Windows.Forms.Control) As Control Implements iTestHarness.SetupControl
        If TypeOf Control Is TextBox Then
            Dim TextBox = DirectCast(Control, TextBox)

            TextBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 12)
            TextBox.Multiline = True
            TextBox.ScrollBars = ScrollBars.Vertical
            TextBox.AppendText(If(TextBox.Text = "", "", vbCrLf & vbCrLf) & "Ths is a standrd text field that uses a dictionary to spel check the its contents ...  as you can se errors are underlnied in red!")

            TextBox.SelectionStart = 0
            TextBox.SelectionLength = 0

            Return TextBox
         ElseIf TypeOf Control Is RichTextBox Then
            Dim RichTextBox = DirectCast(Control, RichTextBox)

            RichTextBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!)

            Dim StartIndex = RichTextBox.TextLength

            RichTextBox.AppendText(If(RichTextBox.Text = "", "", vbCrLf & vbCrLf) & "i00SpellCheck has built in support for RichTextBoxes!" & vbCrLf & _
                               "The quic brown fox junped ovr the lazy dog!" & vbCrLf & _
                               "You can right click to see spelling suggestions for words and to add/ignore/remove words from the dictionary." & vbCrLf & _
                               "If you ignre a word you can hold ctrl down to underlne all ignored words!" & vbCrLf & _
                               "The initial dictionary may take a little while to load ... it holds more than 150 000 words!")

            Dim HighlightKeyWordFormat As New extTextBoxCommon.HighlightKeyWordFormat

            HighlightKeyWordFormat.Color = Color.Red
            extTextBoxCommon.HighlightKeyWord(RichTextBox, "Rich", HighlightKeyWordFormat, RichTextBoxFinds.None, StartIndex)

            HighlightKeyWordFormat.Color = Color.Green
            extTextBoxCommon.HighlightKeyWord(RichTextBox, "Text", HighlightKeyWordFormat, RichTextBoxFinds.None, StartIndex)

            HighlightKeyWordFormat.Color = Color.Blue
            extTextBoxCommon.HighlightKeyWord(RichTextBox, "Boxes", HighlightKeyWordFormat, RichTextBoxFinds.None, StartIndex)

            HighlightKeyWordFormat.Color = Color.FromKnownColor(KnownColor.HotTrack)
            extTextBoxCommon.HighlightKeyWord(RichTextBox, "i00SpellCheck", HighlightKeyWordFormat, RichTextBoxFinds.None, StartIndex)

            HighlightKeyWordFormat.Color = Color.Empty
            HighlightKeyWordFormat.Font = New Font(RichTextBox.Font.Name, CSng(RichTextBox.Font.Size * 1.5), FontStyle.Bold)
            extTextBoxCommon.HighlightKeyWord(RichTextBox, "RichTextBoxes!", HighlightKeyWordFormat, RichTextBoxFinds.None, StartIndex)

            RichTextBox.Select(0, 0)
            RichTextBox.ClearUndo()

            Return RichTextBox
         Else
            Return Nothing
        End If
    End Function

#End Region


8. Finally, here's a bug (!!) in the Drawing.vb source file which can cause an application to crash (!!) when the "character index" at the upper-left or lower-right of a TextBox/RichTextBox is -1. The error is in the CustomPaint procedure, and is corrected as follows:
#Region "Painting"

    Private Sub CustomPaint()
        If OKToDraw = False OrElse parentTextBox.ClientSize.Width = 0 Then Exit Sub

        Dim TextHeight As Integer = System.Windows.Forms.TextRenderer.MeasureText("Ag", parentTextBox.Font).Height
        Dim BufferWidth As Integer = System.Windows.Forms.TextRenderer.MeasureText("--", parentTextBox.Font).Width

        'for drawing underlines below the textbox drawing bounds when on a single line text box
        Dim bHeight = parentTextBox.ClientSize.Height
        If DrawOverlayForm IsNot Nothing Then
            bHeight = DrawOverlayForm.Height
        End If

        Using b As New Bitmap(parentTextBox.ClientSize.Width, bHeight)
            Using g = Graphics.FromImage(b)
                g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

                'Using sb As New SolidBrush(Color.FromArgb(127, Color.Blue))
                '    g.FillRectangle(sb, New RectangleF(0, 0, parentTextBox.Width, parentTextBox.Height))
                'End Using

                Dim FromChar = parentTextBox.GetCharIndexFromPosition(New Point(0, 0))
                If FromChar < 0 Then
                   FromChar = 0
                End If
                Dim ToChar = parentTextBox.GetCharIndexFromPosition(New Point(parentTextBox.ClientRectangle.Width - 1, parentTextBox.ClientRectangle.Height - 1))
                If ToChar < 0 Then
                   ToChar = 0
                End If

                Dim theText As String = Dictionary.Formatting.RemoveWordBreaks(parentTextBox.Text)
                Dim LetterIndex = FromChar
                Dim LeftSide = Left(theText, FromChar)
                LeftSide = LeftSide.Split(" "c).Last
                Dim RightSide = Right(theText, Len(theText) - ToChar)
                RightSide = RightSide.Split(" "c).First
                FromChar -= Len(LeftSide) : ToChar += Len(RightSide)
                Dim VisibleText As String = Mid(theText, FromChar + 1, ToChar - FromChar)

                'If parentTextBox.Multiline = False Then
                'g.TranslateTransform(-System.Windows.Forms.TextRenderer.MeasureText(LeftSide, parentTextBox.Font).Width, -5)
                'End If
                
                Dim NewWords As New Dictionary(Of String, Dictionary.SpellCheckWordError)

                If Trim(VisibleText) <> "" Then
                    Dim words = Replace(Replace(VisibleText, vbCr, " "), vbLf, " ").Split(" "c)

                    For iWord = LBound(words) To UBound(words)
                        If words(iWord) <> "" Then
                            Dim P1 = parentTextBox.GetPositionFromCharIndex(LetterIndex)
                            Dim P1OffsetPlus As Integer = 0
                            If iWord = 0 AndAlso P1.X >= parentTextBox.ClientSize.Width Then
                                P1OffsetPlus = 1
                                P1.X = 0
                            End If
                            If P1.Y < parentTextBox.Height Then
                                Dim WordState As Dictionary.SpellCheckWordError = Dictionary.SpellCheckWordError.SpellError
                                If dictCache.ContainsKey(words(iWord)) Then
                                    'load from cache
                                    WordState = dictCache(words(iWord))
                                Else
                                    ''item is not in the dict cache
                                    If NewWords.ContainsKey(words(iWord)) = False Then
                                        NewWords.Add(words(iWord), Dictionary.SpellCheckWordError.OK)
                                    End If

                                    WordState = Dictionary.SpellCheckWordError.OK
                                End If
                                If WordState = Dictionary.SpellCheckWordError.OK Then

                                Else
                                    If WordState = Dictionary.SpellCheckWordError.Ignore Then
                                        If DrawIgnored() = False Then GoTo ContinueFor
                                    End If

                                    Dim P2 = parentTextBox.GetPositionFromCharIndex(LetterIndex + Len(words(iWord)))
                                    If LeftSide <> "" AndAlso iWord = 0 Then
                                        Dim NormalStringWidth = g.MeasureString(Mid(words(iWord), Len(LeftSide) + 1 + P1OffsetPlus), parentTextBox.Font).Width
                                        Dim XOffsetDiff = g.MeasureString("-" & Mid(words(iWord), Len(LeftSide) + 1 + P1OffsetPlus) & "-", parentTextBox.Font).Width - NormalStringWidth
                                        P2.X = CInt(parentTextBox.GetPositionFromCharIndex(LetterIndex + P1OffsetPlus).X + (NormalStringWidth - XOffsetDiff))
                                    End If
                                    If P2.X = 0 Then
                                        'we are the last char ... :(
                                        P2 = parentTextBox.GetPositionFromCharIndex(LetterIndex + Len(words(iWord)) - 1)
                                        P2.X += System.Windows.Forms.TextRenderer.MeasureText("-" & Right(words(iWord), 1) & "-", parentTextBox.Font).Width - BufferWidth
                                    End If
                                    Dim LineHeight As Integer = extTextBoxCommon.GetLineHeightFromCharPosition(parentTextBox, LetterIndex, RTBContents)
                                    'P1.Y += LineHeight
                                    P2.Y = P1.Y + LineHeight
                                    'P2.Y = P1.Y

                                    Dim e = New SpellCheckCustomPaintEventArgs With {.Graphics = g, .Word = words(iWord), .Bounds = New Rectangle(P1.X, P1.Y, P2.X - P1.X, P2.Y - P1.Y), .WordState = WordState}
                                    OnSpellCheckErrorPaint(e)
                                    If e.DrawDefault Then
                                        Select Case WordState
                                            Case Dictionary.SpellCheckWordError.Ignore
                                                Using p As New Pen(Settings.IgnoreColor)
                                                    g.DrawLine(p, P1.X, P2.Y + 1, P2.X, P2.Y + 1)
                                                End Using
                                            Case Dictionary.SpellCheckWordError.CaseError
                                                DrawingFunctions.DrawWave(g, P1, P2, Settings.CaseMistakeColor)
                                            Case Dictionary.SpellCheckWordError.SpellError
                                                DrawingFunctions.DrawWave(g, P1, P2, Settings.MistakeColor)
                                        End Select
                                    End If
                                End If
                            End If
                        End If
ContinueFor:
                        If LeftSide <> "" AndAlso iWord = 0 Then
                            LetterIndex -= Len(LeftSide)
                        End If
                        LetterIndex += 1 + Len(words(iWord))
                    Next
                End If

Draw:
                If DrawOverlayForm IsNot Nothing Then
                    DrawOverlayForm.SetBitmap(b, 255)
                Else
                    Dim textBoxGraphics = Graphics.FromHwnd(parentTextBox.Handle)
                    textBoxGraphics.DrawImageUnscaled(b, 0, 0)
                End If

                If NewWords.Count > 0 Then
                    AddWordsToCache(NewWords)
                End If

            End Using
        End Using

    End Sub

#End Region




Now re-build the i00SpellCheck project.

PS. You might also want to create a separate, extra solution with only that project. Simply load in the SpellCheck.sln solution, exclude the 11 projects other than i00SpellCheck and save a new solution with a different name, say i00SpellCheck.sln. (Be sure when opening the project alone, though, to click on the new i00SpellCheck.sln file because clicking on the i00SpellCheck.vbproj file will open the entire SpellCheck.sln solution with all 12 projects!)


PS. As a fellow VB programmer--keep the faith!

modified 17-Oct-21 22:24pm.

AnswerADDITIONAL RECOMMENDED CHANGES (for RichTextBox-es) Pin
Robert Gustafson11-Oct-22 16:32
Robert Gustafson11-Oct-22 16:32 
GeneralEVEN MORE ADDITIONAL RECOMMENDED CHANGES !!! Pin
Robert Gustafson19-Feb-23 13:48
Robert Gustafson19-Feb-23 13:48 
QuestionI am using i00 Spell check in my project Pin
Member 1131270020-Sep-19 0:30
Member 1131270020-Sep-19 0:30 
QuestionI am using i00 Spell check in my project Pin
Member 1303975328-Aug-19 14:00
Member 1303975328-Aug-19 14:00 
Questioni00SpellCheck Pin
Member 1075424712-Aug-19 2:49
Member 1075424712-Aug-19 2:49 
QuestionI am using i00 Spell check in my project! And it is awesome! Pin
Member 1288935230-Jun-19 4:11
Member 1288935230-Jun-19 4:11 
QuestionThanks heaps Pin
Member 814715410-Jun-19 1:17
Member 814715410-Jun-19 1:17 
GeneralI am using i00 Spell check in my projec Pin
Member 125797679-Jun-19 9:16
Member 125797679-Jun-19 9:16 
QuestionI am using i00 Spell check in my project! Pin
Member 144795053-Jun-19 14:14
Member 144795053-Jun-19 14:14 
QuestionI am using i00 Spell check in my project Pin
Member 1380685523-May-19 16:32
Member 1380685523-May-19 16:32 
PraiseI am using i00 Spell check in my project Pin
Markus 141697133-Mar-19 12:08
Markus 141697133-Mar-19 12:08 
QuestionProblem only occurred if i changed Windows Scale and Layout to more than 100% Pin
NasserGhoul26-Feb-19 6:23
NasserGhoul26-Feb-19 6:23 
AnswerRe: Problem only occurred if i changed Windows Scale and Layout to more than 100% Pin
i0010-Feb-20 15:18
i0010-Feb-20 15:18 
Question"I am using i00 Spell check in my project" Pin
kopkistre13-Jan-19 1:24
kopkistre13-Jan-19 1:24 
Question"I am using i00 Spell check in my project" Pin
kopkistre13-Jan-19 1:24
kopkistre13-Jan-19 1:24 
QuestionI am using i00 Spell check in my project Pin
steveatquinata21-Dec-18 12:48
steveatquinata21-Dec-18 12:48 
QuestionI am using i00 Spell check in my project Pin
SherMags22-Nov-18 0:52
SherMags22-Nov-18 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.