Example 34 : Spell checking with CheckerStarting from version 1.94 a new mechanism is available to provide cell value checking, error marking and cell autocorrecting components with TAdvStringGrid. This opens the capability to add spell checking components to TAdvStringGrid as in this case an interface is provided to use the Addict Spell Checking
Checking product with TAdvStringGrid.
The main concept is that a TAdvStringGridCheck derived component can be attached to the CellChecker property of TAdvStringGrid. When a Checker component is attached, TAdvStringGrid will call its base methods MarkError and Correct at the right time to make checking and correcting possible after inplace editing is finished or when a programmatic call to the various new Check methods is made.
This is the base class for the Checker component from which all custom checker components must be derived:
TAdvStringGridCheck = class(TComponent) public function MarkError(ACol,ARow: Integer; s:string):string; virtual; function Correct(ACol,ARow: Integer; s:string):string; virtual; procedure StartCheck; virtual; procedure StopCheck; virtual; published property AutoCorrect: Boolean read FAutoCorrect write FAutoCorrect; property AutoMarkError: Boolean read FAutoMarkError write FAutoMarkError; property GotoCell: Boolean read FGotoCell write FGotoCell; property UseCorrect: Boolean read FUseCorrect write FUseCorrect; property UseMarkError: Boolean read FUseMarkError write FUseMarkError; end;
The purpose of the properties AutoCorrect and AutoMarkError is to set whether the Checker component should be used to perform auto correction or auto error marking after editing each cell. The UseCorrect and UseMarkError properties control whether the correction or error marking is used when calling the grid's various Check methods, ie. CheckCell, CheckCells, CheckCol, CheckRow and CheckGrid. Optionally, the GotoCell is used to activate each cell when doing multiple cell checks with the various Check methods to give a visual indication to the user which cell is being checked.
In this base class, the methods Correct and MarkError do nothing. They simply return the cell content as is. With a real checker, these methods should either return the corrected cell's value or the cell's value with markers for words with errors. Error Markers (ie. red line under words with errors) can be applied by using the built-in HighLight function in the base TAdvStringGridCheck component.
As a sample implementation, a Checker component has been provided that does nothing more than capitalize each first letter of a string. The TCapitalCheck component is thus derived from TAdvStringGridCheck and implements only one method, ie. the Correct method in following way:
function TCapitalCheck.Correct(ACol,ARow: Integer;s: string): string; var Prev,PrevPrev:Char; i: Integer; begin Prev := ' '; PrevPrev := '.'; for i := 1 to Length(s) do begin if (Prev = ' ') and (PrevPrev in ['!','?','.']) and (s[i] <> Upcase(s[i])) then s[i] := UpCase(s[i]); PrevPrev := Prev; Prev := s[i]; end; Result := s; end;
It will auto-correct an entered value of "this is a test. i should start with a capital" to "This is a test. I should start with a capital"
Based on this architecture, a component TAddictCheck is provided that uses the Addict Spell Checker to perform spell checking in TAdvStringGrid or other TAdvStringGrid based products. Again this is all possible without writing any code, just drop the Addict Spell Checker components on your form, set all Addict properties to your preferences, add a TAddictCheck component on the form and assign the TAddictSpell component to the TAddictCheck's AddictSpell property. Next, assign the TAddictSpell component to the TAdvStringGrid's CellChecker property. Control with the AutoCorrect, AutoMarkError, GotoCell, UseCorrect, UseMarkError and ShowDialog properties how the Addict Spell Checker should be used with TAdvStringGrid.
Once this is setup, all systems go, and you have a Addict Spell Check enabled grid tailored to your needs.
Notes : The included TAddictCheck component needs to be installed first. It can be installed by adding AsgAddictCheckReg.pas to your TAdvStringGrid package file. The Addict Spell Checker product is required for the sample application and can be found at Addictive Software