// change current word into 'ChangeTo' value void _btnChange_Click(object sender, RoutedEventArgs e) { if (_txtError.Text != _originalText) { // replace whole sentence in editor _editor.Select(_sentence.Start, _sentence.Length); _editor.SelectedText = _txtError.Text; // re-check starting from the sentence start _errors = _spell.CheckText(_editor.Text, _sentence.Start); } else { // save starting point to continue checking from here int start = CurrentError.Start; // get replacement text string replacement = _textChangeTo; if (string.IsNullOrEmpty(replacement)) { // if replacement is empty, expand over spaces and commas CharRange delete = CharRange.ExpandOverWhitespace(_editor.Text, CurrentError); _editor.Select(delete.Start, delete.Text.Length); replacement = string.Empty; } // replace word in text and re-check _editor.SelectedText = replacement; _errors = _spell.CheckText(_editor.Text, start); } // update index ErrorIndex = 0; }
void Benchmark(string text) { // check the text DateTime start = DateTime.Now; CharRangeList notFound = this.c1SpellChecker1.CheckText(text); TimeSpan elapsed = DateTime.Now.Subtract(start); // compute statistics string[] words = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); double seconds = elapsed.TotalSeconds; Dictionary <string, bool> unique = new Dictionary <string, bool>(); foreach (CharRange cr in notFound) { unique[cr.Text] = true; } // show the results _lblNotFound.Text = string.Format("Words not found: {0:n0} ({1:n0} unique)", notFound.Count, unique.Count); _lblTime.Text = string.Format("Time elapsed: {0:n0} milliseconds", elapsed.TotalMilliseconds); _lblWords.Text = string.Format("Words checked: {0:n0}", words.Length); _lblSpeed.Text = string.Format("Words checked per second: {0:n0}", words.Length / elapsed.TotalSeconds); listBox1.BeginUpdate(); listBox1.Items.Clear(); foreach (string word in unique.Keys) { listBox1.Items.Add(word); } listBox1.EndUpdate(); }
//------------------------------------------------------------------------------------------------ #region ** Apply all spelling modes to a RichTextBox // mode 1: batch // check a string and get a list of all spelling mistakes private void btnShowErrorsRich_Click(object sender, EventArgs e) { // get a list with all spelling mistakes CharRangeList errors = this._spell.CheckText(this.richTextBox1.Text); // show the list ShowErrors(errors); }
//------------------------------------------------------------------------ #region ** object model /// <summary> /// Initializes the dialog to use the given parameters. /// </summary> /// <param name="spell"><see cref="C1SpellChecker"/> to use for spelling.</param> /// <param name="editor"><see cref="ISpellCheckableEditor"/> that contains the text to spell-check.</param> /// <param name="errors"><see cref="CharRangeList"/> that contains the initial error list.</param> public void Initialize(C1SpellChecker spell, ISpellCheckableEditor editor, CharRangeList errors) { _spell = spell; _editor = editor; _errors = errors; if (_errors == null) { _errors = _spell.CheckText(_editor.Text); } _errorCount += _errors.Count; }
//------------------------------------------------------------------------------------------------ #region ** utility / event handlers private void ShowErrors(CharRangeList errors) { DataGridView grid = new DataGridView(); grid.DataSource = errors; grid.Dock = DockStyle.Fill; Form f = new Form(); f.StartPosition = FormStartPosition.CenterParent; f.Text = string.Format("{0} Spelling errors detected", errors.Count); f.Controls.Add(grid); f.ShowDialog(); }
//------------------------------------------------------------------------ #region ** object model /// <summary> /// Initializes the dialog to use the given parameters. /// </summary> /// <param name="spell"><see cref="C1SpellChecker"/> to use for spelling.</param> /// <param name="editor"><see cref="ISpellCheckableEditor"/> that contains the text to spell-check.</param> /// <param name="errors"><see cref="CharRangeList"/> that contains the initial error list.</param> public void Initialize(C1SpellChecker spell, ISpellCheckableEditor editor, CharRangeList errors) { // save references to all objects _spell = spell; _editor = editor; _errors = errors; if (_errors == null) { _errors = _spell.CheckText(_editor.Text); } _errorCount += _errors.Count; // go show the first error ErrorIndex = 0; }
//------------------------------------------------------------------------ #region ** object model /// <summary> /// Initializes the dialog to use the given parameters. /// </summary> /// <param name="spell"><see cref="C1SpellChecker"/> to use for spelling.</param> /// <param name="editor"><see cref="ISpellCheckableEditor"/> that contains the text to spell-check.</param> /// <param name="errors"><see cref="CharRangeList"/> that contains the initial error list.</param> public void Initialize(C1SpellChecker spell, ISpellCheckableEditor editor, CharRangeList errors) { // initialize members _spell = spell; _editor = editor; _errors = errors; // initialize 'change all' list _changeAll.Clear(); foreach (string key in _spell.AutoReplaceList.Keys) { _changeAll[key] = _spell.AutoReplaceList[key]; } // initialize error list if (_errors == null) { _errors = _spell.CheckText(_editor.Text); } _errorCount += _errors.Count; }
// change current word into 'ChangeTo' value private void _btnChange_Click(object sender, EventArgs e) { // save starting point to continue checking from here int start = CurrentError.Start; // get replacement text string replacement = _txtChangeTo.Text; if (replacement.Length == 0) { // if replacement is empty, expand over spaces and commas CharRange delete = CharRange.ExpandOverWhitespace(_editor.Text, CurrentError); _editor.Select(delete.Start, delete.Text.Length); } // replace word in text and re-check _editor.SelectedText = replacement; _errors = _spell.CheckText(_editor.Text, start); // update index ErrorIndex = 0; }
// add current word to the ignore list in the spell-checker private void _btnIgnoreAll_Click(object sender, EventArgs e) { _spell.IgnoreList.Add(CurrentError.Text); _errors = _spell.CheckText(_editor.Text, CurrentError.Start); UpdateCurrentError(); }
//------------------------------------------------------------------------ #region ** private stuff // update dialog to show current error private void UpdateCurrentError() { // design time if (DesignMode || _errors == null) { return; } // finished with this batch of errors if (ErrorIndex >= _errors.Count) { // check whether the editor has more text to check while (_editor.HasMoreText()) { _errors = _spell.CheckText(_editor.Text); if (_errors.Count > 0) { _errorCount += _errors.Count; ErrorIndex = 0; return; } } // editor has no more text... DialogResult = DialogResult.OK; return; } // update current error CharRange err = CurrentError; // select word in editor _editor.Select(err.Start, err.Text.Length); // honor 'change all' list if (_changeAll.ContainsKey(err.Text)) { _txtChangeTo.Text = _changeAll[err.Text]; _btnChange_Click(this, EventArgs.Empty); return; } // show bad word, new text _txtError.Text = err.Text; _txtChangeTo.Text = string.Empty; // repeated word? if (err.Duplicate) { // adjust dialog _lblNotInDictionary.Visible = false; _lblRepeatedWord.Visible = true; _btnIgnoreAll.Enabled = false; _btnChangeAll.Enabled = false; _btnAdd.Enabled = false; // no suggestions _listSuggestions.Items.Clear(); } else { // adjust dialog _lblRepeatedWord.Visible = false; _lblNotInDictionary.Visible = true; _btnIgnoreAll.Enabled = true; _btnChangeAll.Enabled = true; _btnAdd.Enabled = true; // show suggestions string[] suggestions = _spell.GetSuggestions(err.Text); _listSuggestions.Items.Clear(); if (suggestions.Length > 0) { _listSuggestions.Items.AddRange(suggestions); _listSuggestions.SelectedIndex = 0; } else { _listSuggestions.Items.Add(_lblNoSuggestions.Text); _listSuggestions.SelectedIndex = -1; } } // focus to new word _txtChangeTo.SelectAll(); _txtChangeTo.Focus(); _btnSuggest.Enabled = false; AcceptButton = _btnIgnore; // show 'Add' button only if user dictionary is enabled _btnAdd.Visible = _spell.UserDictionary.Enabled; // all ready, fire ErrorDisplayed event OnErrorDisplayed(EventArgs.Empty); }
// change current word into 'ChangeTo' value private void _btnChange_Click(object sender, EventArgs e) { // save starting point to continue checking from here int start = CurrentError.Start; // if the user typed into the text error box, handle that CharRange replacement = null; if (ReplacementTextChanged && _listSuggestions.Enabled == false) { // get part of the text that replaced the actual error within the sentence replacement = GetReplacement(); if (replacement != null && replacement.Text.Length > 0) { // warn if replacement is bad if (_spell.CheckText(replacement.Text).Count > 0) { string msg = "You have chosen a word that is not in the dictionary.\r\n" + "Do you want to use this word and continue checking?"; DialogResult dr = MessageBox.Show(this, msg, "Spelling", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.No) { return; } } // honor ChangeAll button if (sender.Equals(_btnChangeAll)) { _changeAll[CurrentError.Text] = replacement.Text; } } // replace whole sentence in text _editor.Select(_sentence.Start, _sentence.Length); _editor.SelectedText = _txtError.Text; // resume checking from sentence start start = _sentence.Start; } else { // no changes in text, use Suggestion/ChangeAll value stored in _txtChangeTo string changeTo = _textChangeTo; if (changeTo.Length == 0) { // if replacement is empty, expand over spaces and commas CharRange delete = CharRange.ExpandOverWhitespace(_editor.Text, CurrentError); _editor.Select(delete.Start, delete.Text.Length); } // honor ChangeAll button if (sender.Equals(_btnChangeAll)) { _changeAll[CurrentError.Text] = changeTo; } // replace word in text _editor.SelectedText = changeTo; _errors = _spell.CheckText(_editor.Text); } // re-check _errors = _spell.CheckText(_editor.Text); _errorIndex = -1; for (int index = 0; index < _errors.Count; index++) { if (_errors[index].Start >= start) { if (replacement == null || replacement.Text != _errors[index].Text) { _errorIndex = index; break; } } } // show the current error UpdateCurrentError(); }
//------------------------------------------------------------------------ #region ** private stuff // update dialog to show current error void UpdateCurrentError() { // design time if (_errors == null) { return; } // finished with this batch of errors if (ErrorIndex >= _errors.Count) { // check whether the editor has more text to check while (_editor.HasMoreText()) { _errors = _spell.CheckText(_editor.Text); if (_errors.Count > 0) { _errorCount += _errors.Count; ErrorIndex = 0; return; } } // editor has no more text... DialogResult = MessageBoxResult.OK; return; } // update current error CharRange err = CurrentError; // select word in editor _editor.Select(err.Start, err.Text.Length); // honor 'change all' list if (_changeAll.ContainsKey(err.Text)) { _textChangeTo = _changeAll[err.Text]; _btnChange_Click(this, new RoutedEventArgs()); return; } // raise 'BadWordFound' event BadWordEventArgs e = new BadWordEventArgs(this, _editor as Control, err, _errors); _spell.OnBadWordFound(e); if (e.Cancel) { DialogResult = MessageBoxResult.Cancel; return; } // show whole sentence, highlight bad word _updatingText = true; _sentence = GetSentence(_editor.Text, err); _originalText = _sentence.Text; _txtError.FontFamily = _editor.Control.FontFamily; _txtError.Text = _sentence.Text; _txtError.Select(err.Start - _sentence.Start, err.Length); _txtError.Selection.FontWeight = FontWeights.Bold; _txtError.Selection.Foreground = _errorForeground; _updatingText = false; // repeated word? if (err.Duplicate) { // adjust dialog _lblNotInDictionary.Visibility = Visibility.Collapsed; _lblRepeatedWord.Visibility = Visibility.Visible; _btnIgnoreAll.IsEnabled = false; _btnChangeAll.IsEnabled = false; _btnAdd.IsEnabled = false; // no suggestions _listSuggestions.Items.Clear(); } else { // adjust dialog _lblRepeatedWord.Visibility = Visibility.Collapsed; _lblNotInDictionary.Visibility = Visibility.Visible; _btnIgnoreAll.IsEnabled = true; _btnChangeAll.IsEnabled = true; _btnAdd.IsEnabled = true; // show suggestions UpdateSuggestions(err.Text); } // focus to new word _txtError.Focus(); _btnSuggest.IsEnabled = false; AcceptButton = _btnIgnore; // show 'Add' button only if user dictionary is enabled _btnAdd.Visibility = _spell.UserDictionary.Enabled ? Visibility.Visible : Visibility.Collapsed; // update button status UpdateButtonStatus(); // all ready, fire ErrorDisplayed event OnErrorDisplayed(EventArgs.Empty); }
// change current word into 'ChangeTo' value void _btnChange_Click(object sender, RoutedEventArgs e) { // save starting point to continue checking from here int start = CurrentError.Start; // get replacement text string replacement = _txtChangeTo.Text; if (replacement.Length == 0) { // if replacement is empty, expand over spaces and commas CharRange delete = CharRange.ExpandOverWhitespace(_editor.Text, CurrentError); _editor.Select(delete.Start, delete.Text.Length); } // replace word in text and re-check _editor.SelectedText = replacement; _errors = _spell.CheckText(_editor.Text, start); // update index ErrorIndex = 0; }
//------------------------------------------------------------------------ #region ** private stuff // update dialog to show current error private void UpdateCurrentError() { // design time if (DesignMode || _errors == null) { return; } // finished with this batch of errors if (ErrorIndex < 0 || ErrorIndex >= _errors.Count) { // check whether the editor has more text to check while (_editor.HasMoreText()) { _errors = _spell.CheckText(_editor.Text); if (_errors.Count > 0) { _errorCount += _errors.Count; ErrorIndex = 0; return; } } // editor has no more text... DialogResult = DialogResult.OK; return; } // update current error CharRange err = CurrentError; // select word in editor _editor.Select(err.Start, err.Text.Length); // honor 'change all' list if (_changeAll.ContainsKey(err.Text)) { _textChangeTo = _changeAll[err.Text]; _btnChange_Click(this, EventArgs.Empty); return; } // show whole sentence, highlight bad word _sentence = GetSentence(_editor.Text, err); _txtError.Font = _editor.Control.Font; _txtError.Text = _sentence.Text; _txtError.Select(err.Start - _sentence.Start, err.Length); _txtError.SelectionFont = new Font(_txtError.Font, FontStyle.Bold); _txtError.SelectionColor = _errorForeColor; _txtError.SelectionBackColor = _errorBackColor; _txtError.Select(_txtError.SelectionStart + _txtError.SelectionLength, 0); // repeated word? if (err.Duplicate) { // adjust dialog _lblNotInDictionary.Visible = false; _lblRepeatedWord.Visible = true; _btnChange.Text = _lblRemove.Text; _btnIgnoreAll.Enabled = false; _btnChangeAll.Enabled = false; _btnAdd.Enabled = false; // no suggestions _listSuggestions.Items.Clear(); } else { // adjust dialog _lblRepeatedWord.Visible = false; _lblNotInDictionary.Visible = true; _btnChange.Text = _lblChange.Text; _btnIgnoreAll.Enabled = true; _btnChange.Enabled = false; _btnChangeAll.Enabled = false; _btnAdd.Enabled = true; // show suggestions ShowSuggestions(err.Text); } // focus to new word _txtError.Focus(); _btnSuggest.Enabled = false; AcceptButton = _btnIgnore; // show 'Add' button only if user dictionary is enabled _btnAdd.Visible = _spell.UserDictionary.Enabled; // ready, fire BadWordDetected event BadWordEventArgs e = new BadWordEventArgs(this, _editor.Control, err, _errors); _spell.OnBadWordFound(e); // ignore error on event-handler request if (e.Cancel) { ErrorIndex++; } }
// update dialog to show current error void UpdateCurrentError() { // design time if (_errors == null) { return; } // finished with this batch of errors if (ErrorIndex >= _errors.Count) { // check whether the editor has more text to check while (_editor.HasMoreText()) { _errors = _spell.CheckText(_editor.Text); if (_errors.Count > 0) { _errorCount += _errors.Count; ErrorIndex = 0; return; } } // editor has no more text... DialogResult = MessageBoxResult.OK; return; } // update current error CharRange err = CurrentError; // select word in editor _editor.Select(err.Start, err.Text.Length); // honor 'change all' list if (_changeAll.ContainsKey(err.Text)) { _txtChangeTo.Text = _changeAll[err.Text]; _btnChange_Click(this, new RoutedEventArgs()); return; } // raise 'BadWordFound' event BadWordEventArgs e = new BadWordEventArgs(this, _editor as Control, err, _errors); _spell.OnBadWordFound(e); if (e.Cancel) { DialogResult = MessageBoxResult.Cancel; return; } // show bad word, new text _txtError.Text = err.Text; _txtChangeTo.Text = string.Empty; // repeated word? if (err.Duplicate) { // adjust dialog _lblNotInDictionary.Visibility = Visibility.Collapsed; _lblRepeatedWord.Visibility = Visibility.Visible; _btnIgnoreAll.IsEnabled = false; _btnChangeAll.IsEnabled = false; _btnAdd.IsEnabled = false; // no suggestions _listSuggestions.Items.Clear(); } else { // adjust dialog _lblRepeatedWord.Visibility = Visibility.Collapsed; _lblNotInDictionary.Visibility = Visibility.Visible; _btnIgnoreAll.IsEnabled = true; _btnChangeAll.IsEnabled = true; _btnAdd.IsEnabled = true; // show suggestions UpdateSuggestions(err.Text); } // focus to new word _txtChangeTo.SelectAll(); _txtChangeTo.Focus(); _btnSuggest.IsEnabled = false; AcceptButton = _btnIgnore; // show 'Add' button only if user dictionary is enabled _btnAdd.Visibility = _spell.UserDictionary.Enabled ? Visibility.Visible : Visibility.Collapsed; // update button status UpdateButtonStatus(); // all ready, fire ErrorDisplayed event OnErrorDisplayed(EventArgs.Empty); }
/// <summary> /// Initializes the dialog to use the given parameters. /// </summary> /// <param name="spell"><see cref="C1SpellChecker"/> to use for spelling.</param> /// <param name="editor"><see cref="ISpellCheckableEditor"/> that contains the text to spell-check.</param> /// <param name="errors"><see cref="CharRangeList"/> that contains the initial error list.</param> public void Initialize(C1SpellChecker spell, ISpellCheckableEditor editor, CharRangeList errors) { // save references to all objects _spell = spell; _editor = editor; _errors = errors; if (_errors == null) { _errors = _spell.CheckText(_editor.Text); } _errorCount += _errors.Count; // go show the first error ErrorIndex = 0; }
//------------------------------------------------------------------------------------------------ #region ** Apply all spelling modes to a regular TextBox (exactly the same code as before) private void btnShowErrors_Click(object sender, EventArgs e) { CharRangeList errors = this._spell.CheckText(this.textBox1.Text); ShowErrors(errors); }
// add current word to the user dictionary private void _btnAdd_Click(object sender, EventArgs e) { _spell.UserDictionary.AddWord(CurrentError.Text); _errors = _spell.CheckText(_editor.Text); UpdateCurrentError(); }
// add current word to the user dictionary private void _btnAdd_Click(object sender, EventArgs e) { _spell.UserDictionary.AddWord(CurrentError.Text); _errors = _spell.CheckText(_editor.Text, CurrentError.Start); ErrorIndex = 0; }
// add current word to the user dictionary void _btnAdd_Click(object sender, RoutedEventArgs e) { _spell.UserDictionary.AddWord(CurrentError.Text); _errors = _spell.CheckText(_editor.Text, CurrentError.Start); ErrorIndex = 0; }
// add current word to the ignore list in the spell-checker void _btnIgnoreAll_Click(object sender, RoutedEventArgs e) { _spell.IgnoreList.Add(CurrentError.Text); _errors = _spell.CheckText(_editor.Text, CurrentError.Start); UpdateCurrentError(); }