Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / WPF
Tip/Trick

AvalonEdit and Spell check

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
14 Mar 2013GPL32 min read 23.3K   837   9   5
Quick hack for spell check with AvalonEdit.

Introduction  

As the name of the article implies, this is a quick hack to add spell check in your application using the brilliant AvalonEdit. This is a quick tricky/dirty way to implement spell check using the built-in spell checker in .NET without relying on 3rd party libraries. Note that this might not be the most efficient way of spell check. 

Background  

AvalonEdit is a popular open source text editor control used in #D and widely used by many open source developers in their open source projects. When I started using AvalonEdit, I wanted to add spell check in my application. Initially, I was under the assumption that AvalonEdit inherits from TextBoxBase which means that I can just use the inbuilt spell checker. However that is not the case - AvalonEdit inherits from Control.

The Idea 

In my implementation (See source code) - there are two pieces of development: 

  1. Every AvalonEdit text editor control has a corresponding TextBox control exclusively to get spelling errors. Every change to the text editor will also update the text box - thereby letting the application get the spelling errors. This has been implemented by creating a behavior called SpellCheckBehavior for AvalonEdit.
  2. A new DocumentColorizingTransformer called the SpellingErrorColorizer, is available as a part of the code. This needs to be added to the list of line transformers in the text view of your editor. The idea behind this transformer is using a static text box which can give the errors on the DocumentLine that needs to be processed. 

The Result

Using the code

Download the code and have a look at it as it is self explanatory. 

In MainWindow.xaml, create an instance of the text editor and add the behavior. 

<avalonedit:TextEditor Name="textEditor" FontSize="20">
    <i:Interaction.Behaviors>
        <SpellCheckAvalonEdit:SpellCheckBehavior />
    </i:Interaction.Behaviors>
</avalonedit:TextEditor>  

In MainWindow.xaml.cs, add the SpellingErrorColorizer to the line transformers.

public MainWindow()
{
    InitializeComponent();
    textEditor.TextArea.TextView.LineTransformers.Add(new SpellingErrorColorizer());
} 

Note: In this implementation, I did not care about the performance. If you have a better suggestions, please share/contribute. 

History 

  • 03/11/13 - First spell check idea.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
United States United States
Hello! I'm a 26 year old, WPF/C# self-learned software developer, musician from India with a Master's degree in Computer science. I believe that every piece of software and information/knowledge is available for free for those who seek it.

My projects are available in my Webpage hosted on Github. I am eager to learn new things everyday, explore new technologies and solve problems.

Comments and Discussions

 
QuestionIs MSWord on machine is needed where this application will run? Pin
HM India1-Aug-13 12:36
professionalHM India1-Aug-13 12:36 
AnswerRe: Is MSWord on machine is needed where this application will run? Pin
Chandramouleswaran1-Aug-13 15:50
Chandramouleswaran1-Aug-13 15:50 
QuestionDownload unavailable Pin
Dirk Bahle14-Mar-13 2:09
Dirk Bahle14-Mar-13 2:09 
AnswerRe: Download unavailable Pin
Chandramouleswaran14-Mar-13 4:48
Chandramouleswaran14-Mar-13 4:48 
GeneralRe: Download unavailable Pin
Dirk Bahle15-Mar-13 10:43
Dirk Bahle15-Mar-13 10:43 

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.