<form
id="SpellCheckForm"
runat="server">
<asp:CustomValidator
ID="spellCheckValidator"
OnServerValidate="SpellCheck"
runat="server"
ErrorMessage="*"
ControlToValidate="SpellCheckedTextBox"
SetFocusOnError="True"
EnableClientScript="False"
EnableViewState="True"></asp:CustomValidator>
<asp:TextBox
TextMode="MultiLine"
ID="SpellCheckedTextBox"
runat="server"
Width="95%"
Rows="10"></asp:TextBox>
<br
/>
<br
/>
<asp:Button
ID="SubmitButton"
runat="server"
Text="Submit"
OnClick="SubmitButton_Click"
/>
</form>
protected void SpellCheck(object sender, ServerValidateEventArgs args)
{
SpellCheckHelper sh = new SpellCheckHelper();
CustomValidator validator = (CustomValidator)sender;
sh.BeginSpellCheck((string)args.Value);
Dictionary<int, string[]> spellingErrors;
spellingErrors = sh.EndSpellCheck();
args.IsValid = (spellingErrors.Count == 0) ? true : false;
if (!args.IsValid)
{
foreach (int key in spellingErrors.Keys)
{
string currentError = spellingErrors[key][0];
string suggestionsText = "";
if (spellingErrors[key].Length > 1)
{
for (int i = 1; i < spellingErrors[key].Length; i++)
{
suggestionsText += string.Format("<li><i>{0}?</i></li>", spellingErrors[key][i]);
}
}
validator.Text +=
string.Format("<li>Spelling error at character {0}: {1}<ul>{2}</ul></li>",
key, currentError, suggestionsText);
}
validator.Text += "</ul>";
}
return;
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
}
using System;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Threading;
namespace QuickLearn.Blog.Samples
{
public class SpellCheckHelper
{
public SpellCheckHelper()
{
}
private readonly object sync = new object();
private string text;
private Dictionary<int, string[]> spellingErrors;
private bool isComplete;
public bool IsComplete { get { return isComplete; } }
private bool inProgress;
private Exception lastException = null;
public void BeginSpellCheck(string text)
{
if (inProgress) throw new InvalidOperationException("Spell check is already in progress.");
this.text = text;
this.spellingErrors = new Dictionary<int, string[]>();
Thread backgroundThread = new Thread(new ThreadStart(doSpellCheck));
backgroundThread.SetApartmentState(ApartmentState.STA);
backgroundThread.Start();
isComplete = false;
inProgress = true;
}
public Dictionary<int, string[]> EndSpellCheck()
{
if (!inProgress) throw new InvalidOperationException("Spell check is not in progress.");
lock (sync)
{
if (!isComplete)
{
Monitor.Wait(sync);
}
}
isComplete = false;
inProgress = false;
if (this.lastException != null)
{
Exception ex = this.lastException;
this.lastException = null;
throw ex;
}
return this.spellingErrors;
}
private void doSpellCheck()
{
try
{
System.Windows.Controls.TextBox wpfTextBox = new System.Windows.Controls.TextBox();
wpfTextBox.AcceptsReturn = true;
wpfTextBox.AcceptsTab = true;
wpfTextBox.SpellCheck.IsEnabled = true;
wpfTextBox.Text = this.text;
int index = 0;
while ((index = wpfTextBox.GetNextSpellingErrorCharacterIndex(index, System.Windows.Documents.LogicalDirection.Forward)) != -1)
{
string currentError = wpfTextBox.Text.Substring(index, wpfTextBox.GetSpellingErrorLength(index));
List<string> suggestions = new List<string>();
suggestions.Add(currentError);
foreach (string suggestion in wpfTextBox.GetSpellingError(index).Suggestions)
{
suggestions.Add(suggestion);
}
this.spellingErrors.Add(index, suggestions.ToArray());
index += currentError.Length;
}
}
catch (Exception ex)
{
this.lastException = ex;
}
finally
{
lock (sync)
{
isComplete = true;
Monitor.PulseAll(sync);
}
}
}
}
}
All sample code provided AS-IS with no warranties.