|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpellChecker;
using SpellChecker.Forms;
namespace NNControl.NNTesting
{
public partial class TextSpellControl : UserControl
{
#region private properties
private SpellChecker.MultipleSpelling spellChecker;
List<String> suggestionList;
#endregion
#region public properties
public SpellChecker.MultipleSpelling SpellChecker
{
get
{
return spellChecker;
}
set
{
if (spellChecker == value)
return;
spellChecker = value;
}
}
public String SpellCheckingText
{
get
{
return TextBeingChecked.Text;
}
set
{
spellChecker.Text = value;
spellChecker.WordIndex = 0;
TextBeingChecked.Text = value;
TextBeingChecked.ReadOnly = true;
spellChecker.SpellCheck();
}
}
#endregion
#region constructor
public TextSpellControl()
{
InitializeComponent();
suggestionList = null;
splitContainer.Panel2Collapsed = true;
spellChecker = null;
}
#endregion
#region public methods
#endregion
#region Spelling Events
private void SpellChecker_DoubledWord(object sender, SpellChecker.SpellingEventArgs e)
{
this.UpdateDisplay(this.spellChecker.Text, e.Word,
e.WordIndex, e.TextIndex);
//turn off ignore all option on double word
this.ignoreAllToolStripMenuItem.Enabled = false;
this.addToDictionaryToolStripMenuItem.Enabled = false;
}
private void SpellChecker_EndOfText(object sender, System.EventArgs e)
{
this.UpdateDisplay(this.spellChecker.Text, "", 0, 0);
if (this.spellChecker.AlertComplete)
{
MessageBox.Show(this, "Spell Check Complete.", "Spell Check",
MessageBoxButtons.OK, MessageBoxIcon.Information);
//hide SuggestionToolStrip
if (suggestionList != null)
{
suggestionList.Clear();
}
suggestiontoolStrip.Items.Clear();
splitContainer.Panel2Collapsed = true;
TextBeingChecked.ReadOnly = false;
//turn off
this.ignoreAllToolStripMenuItem.Enabled = false;
this.ignoretoolStripMenuItem.Enabled = false;
this.addToDictionaryToolStripMenuItem.Enabled = false;
}
}
private void SpellChecker_MisspelledWord(object sender, SpellChecker.SpellingEventArgs e)
{
//turn on ignore all option
this.ignoreAllToolStripMenuItem.Enabled = true;
this.ignoretoolStripMenuItem.Enabled = true;
this.addToDictionaryToolStripMenuItem.Enabled = true;
//generate suggestions
if (spellChecker != null)
{
spellChecker.Suggest();
//update suggestions
if (suggestionList != null)
{
suggestionList.Clear();
}
else
{
suggestionList = new List<String>();
}
suggestionList.AddRange((string[])spellChecker.Suggestions.ToArray(typeof(string)));
this.UpdateDisplay(this.spellChecker.Text, e.Word,
e.WordIndex, e.TextIndex);
}
}
private void UpdateDisplay(string text, string word, int wordIndex, int textIndex)
{
//set text context
this.TextBeingChecked.ResetText();
this.TextBeingChecked.SelectionColor = Color.Black;
if (word.Length > 0)
{
//highlight current word
this.TextBeingChecked.AppendText(text.Substring(0, textIndex));
this.TextBeingChecked.SelectionColor = Color.Red;
this.TextBeingChecked.AppendText(word);
this.TextBeingChecked.SelectionColor = Color.Black;
this.TextBeingChecked.AppendText(text.Substring(textIndex + word.Length));
//set caret and scroll window
this.TextBeingChecked.Select(textIndex, 0);
this.TextBeingChecked.Focus();
this.TextBeingChecked.ScrollToCaret();
}
else
{
this.TextBeingChecked.AppendText(text);
}
wordIndex++; //WordIndex is 0 base, display is 1 based
//display suggestion toolStrip
suggestiontoolStrip.Items.Clear();
if (suggestionList != null && suggestionList.Count > 0)
{
splitContainer.Panel2Collapsed = false;
foreach (var suggest in suggestionList)
{
ToolStripDropDownButton dropdownButton = new ToolStripDropDownButton();
dropdownButton.Text = suggest;
dropdownButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
ToolStripMenuItem replaceItem = new ToolStripMenuItem();
replaceItem.Text = "Replace";
replaceItem.Click += new System.EventHandler(this.replaceToolStripMenuItem_Click);
ToolStripMenuItem replaceAllItem = new ToolStripMenuItem();
replaceAllItem.Text = "Replace All";
replaceAllItem.Click += new System.EventHandler(this.replaceAllToolStripMenuItem_Click);
dropdownButton.DropDownItems.Add(replaceItem);
dropdownButton.DropDownItems.Add(replaceAllItem);
suggestiontoolStrip.Items.Add(dropdownButton);
}
}
}
/// <summary>
/// called by spell checker to enable this
/// form to handle spell checker events
/// </summary>
internal void AttachEvents()
{
if (spellChecker != null)
{
spellChecker.MisspelledWord += new MultipleSpelling.MisspelledWordEventHandler(this.SpellChecker_MisspelledWord);
spellChecker.DoubledWord += new MultipleSpelling.DoubledWordEventHandler(this.SpellChecker_DoubledWord);
spellChecker.EndOfText += new MultipleSpelling.EndOfTextEventHandler(this.SpellChecker_EndOfText);
}
}
/// <summary>
/// called by spell checker to disable this
/// form from handling spell checker events
/// </summary>
internal void DetachEvents()
{
spellChecker.MisspelledWord -= new MultipleSpelling.MisspelledWordEventHandler(this.SpellChecker_MisspelledWord);
spellChecker.DoubledWord -= new MultipleSpelling.DoubledWordEventHandler(this.SpellChecker_DoubledWord);
spellChecker.EndOfText -= new MultipleSpelling.EndOfTextEventHandler(this.SpellChecker_EndOfText);
}
#endregion
#region Control Events
private void replaceAllToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
ToolStripItem owner = item.OwnerItem;
String replacementWord = owner.Text;
this.spellChecker.ReplaceAllWord(replacementWord);
this.TextBeingChecked.Text = this.spellChecker.Text;
this.spellChecker.SpellCheck();
}
private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
ToolStripItem owner = item.OwnerItem;
String replacementWord = owner.Text;
this.spellChecker.ReplaceWord(replacementWord);
this.TextBeingChecked.Text = this.spellChecker.Text;
this.spellChecker.SpellCheck();
}
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
/*
OptionForm options = new OptionForm(ref spellChecker);
options.ShowDialog(this);
if (options.DialogResult == DialogResult.OK)
{
this.SpellChecker.SpellCheck();
}
*/
}
private void ignoretoolStripMenuItem_Click(object sender, EventArgs e)
{
this.spellChecker.IgnoreWord();
this.spellChecker.SpellCheck();
}
private void ignoreAllToolStripMenuItem_Click(object sender, EventArgs e)
{
this.spellChecker.IgnoreAllWord();
this.spellChecker.SpellCheck();
}
private void addToDictionaryToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void TextSpellControl_Load(object sender, EventArgs e)
{
AttachEvents();
}
private void spellingRecheckToolStripMenuItem_Click(object sender, EventArgs e)
{
spellChecker.Text = TextBeingChecked.Text;
TextBeingChecked.ReadOnly = true;
spellChecker.SpellCheck();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.