Click here to Skip to main content
13,138,857 members (53,035 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 1 May 2012

Spell Checker Web Service - Using a WPF TextBox

, 1 May 2012
Rate this:
Please Sign up or sign in to vote.
This tip demonstrates the way we can use WPF Textbox to check for spelling errors and using it via web service.


Spelling mistakes are common. Fixing them is somewhat a pain and fixing it automatically (or at least getting suggestions is even more) pain. This tip aims to provide a way to do spell checking. I have seen numerous posts online where many (or almost all) ask to create a Word Interop object and provide spell checks. It was essential for the server to be having Word installed on it. I always found it rather cumbersome to do this task. I always thought that there must be a simpler way to do it. One day, I needed a web service to check out the spelling errors. The requirement for a web service was as it was going to be checked on cross platform.


I began to dive into the framework itself to get some hints. After looking up MSDN, I found that WPF TextBox can check spellings. Hmm... Interesting, I thought. But is it possible to incorporate a textbox in a web service? Haven't done that before but that didn't let me down. I tried incorporating the TextBox. I had to add reference to a couple of assemblies before I could do that. I added the necessary assembly references and it was built successfully! I had a strange feeling that it's like silence before the storm! And yes! I was right. Though it built successfully, it had a runtime error which stated that WPF Component cannot be created on a MTA (Multi Threaded Application) based application! Before losing all hopes, I thought I must give it a try once more by searching if there is any way to convert the webservice into a STA (Single Threaded Application). The answer was yes! The online MSDN Magazine has a great article regarding the same! Wow I was really happy to see that article which of course was of great help. In case you want to have a look at it, check it out here.

Now that I had my code being shifted to STA, I kept my fingers crossed and pressed the heavenly buttons Ctrl + F5... Building... Build Succeeded... and a page which prompted me to execute the method I want... Click on the method, wow now I am getting a prompt to enter the text (let's hope everything is fine now...) and Invoke! Wow, this worked! This is really working!

The Solution

The web service currently has two methods. The first one is called GetSuggestionForWord which takes in a string with just a single word to get the suggestion for the word. The second one is called GetSuggestionForSentence which takes in a sentence and provides suggestion per word basis.

Following is the method GetSuggestionForWord

public List<string> GetSuggestionForWord(string word)
  TextBox objTextBox = new TextBox(); //Creating an instance of the WPF Textbox.
  objTextBox.Text = word; //Setting the text property of it to the word which came.
  objTextBox.SpellCheck.IsEnabled = true; //Changing the spellcheck to enable.
  var spellingErrors = objTextBox.GetSpellingError(0); //Get the spelling error if any at index 0.
  if (spellingErrors != null) //If spelling errors is not null we have an error.
    IEnumerable<string> spellSuggestions = spellingErrors.Suggestions; //Getting all the suggestions
    return new List<string>(spellSuggestions); //Returning a list of string which has the suggestions.
  else //else we return null.
    return null;

Following is the method GetSuggestionForSentence:

public List<WordSuggestion> GetSuggestionForSentence(string sentence)
  TextBox objTextBox = new TextBox(); //Creating an instance of WPF Textbox.
  objTextBox.SpellCheck.IsEnabled = true; //Enabling spellcheck.
  var words = sentence.Split(new char[] { ' ', ',', ';', '.' }); //splitting the words from the sentences.
  //Creating a generic list of WordSuggestion class.
  List<WordSuggestion> suggestions = new List<WordSuggestion>();
  foreach (var item in words) //iterating over the words
    objTextBox.Text = item; //setting the text property of the textbox to the current word.
    var spellErrors = objTextBox.GetSpellingError(0); //getting the spelling error if any.
    if (spellErrors != null) //if we have a spelling mistake
       suggestions.Add(new WordSuggestion
                              Suggestions = new List<string>(spellErrors.Suggestions),
                              Word = item
                           }); //adding the current word and suggestion.
  return suggestions.Count > 0 ? suggestions : null; //If the suggestions count is greater than one, return list else return null.

Points of Interest

It was really interesting to find that we have to make the service a STA. I wonder if that affects the scalability in some or other manner. I am also concerned about creation of such instances on the web server. How does it scale? Is the biggest question in my mind right now. This is my first article on CodeProject. I would be happy if given feedback regarding the same. Positive / Negative, welcome!


  • Version 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pankaj Nikam
Software Developer
India India
Currently a .NET Developer. Passionate about Microsoft .NET Technologies.

You may also be interested in...

Comments and Discussions

QuestionSpellchecker Pin
Member 117888991-Sep-15 10:40
memberMember 117888991-Sep-15 10:40 
AnswerRe: Spellchecker Pin
Pankaj Nikam1-Sep-15 19:30
professionalPankaj Nikam1-Sep-15 19:30 
QuestionSpellChecker Pin
nik kamble11-Feb-14 19:59
groupnik kamble11-Feb-14 19:59 
AnswerRe: SpellChecker Pin
Pankaj Nikam11-Feb-14 22:07
memberPankaj Nikam11-Feb-14 22:07 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 1 May 2012
Article Copyright 2012 by Pankaj Nikam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid