private void _btnBatch_Click(object sender, RoutedEventArgs e) { // check a word foreach (string word in "yes;no;yesno;do;don't;ain't;aint".Split(';')) { WriteLine("CheckWord(\"{0}\") = {1}", word, _c1SpellChecker.CheckWord(word)); } // check some text var someText = "this text comtains two errrors."; var errors = _c1SpellChecker.CheckText(someText); WriteLine("CheckText(\"{0}\") =", someText); foreach (var error in errors) { WriteLine("\t{0}, {1}-{2}", error.Text, error.Start, error.Length); foreach (string suggestion in _c1SpellChecker.GetSuggestions(error.Text, 1000)) { WriteLine("\t\t{0}?", suggestion); } } // get suggestions var badWord = "traim"; WriteLine("GetSuggestions(\"{0}\")", badWord); foreach (string suggestion in _c1SpellChecker.GetSuggestions(badWord, 1000)) { WriteLine("\t{0}?", suggestion); } }
private void ShowSuggestions(string word) { string[] suggestions = _spell.GetSuggestions(word); _listSuggestions.Items.Clear(); if (suggestions.Length > 0) { _listSuggestions.Items.AddRange(suggestions); _listSuggestions.SelectedIndex = 0; _listSuggestions.Enabled = true; } else { _listSuggestions.Items.Add(_lblNoSuggestions.Text); _listSuggestions.SelectedIndex = -1; _listSuggestions.Enabled = false; } }
// update suggestions in the list void UpdateSuggestions(string word) { _listSuggestions.Items.Clear(); string[] suggestions = _spell.GetSuggestions(word); if (suggestions.Length > 0) { foreach (var suggestion in suggestions) { AddSuggestion(suggestion); } _listSuggestions.SelectedIndex = 0; _listSuggestions.IsEnabled = true; } else { AddSuggestion(_lblNoSuggestions.Text); _listSuggestions.SelectedIndex = -1; _listSuggestions.IsEnabled = false; } }
//------------------------------------------------------------------------ #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); }
// show suggestions when user clicks an error protected override void OnContextMenuOpening(System.Windows.Controls.ContextMenuEventArgs e) { // get item that was clicked Point p = new Point(e.CursorLeft, e.CursorTop); var pointer = GetPositionFromPoint(p, true); if (pointer == null) { e.Handled = true; return; } // get error at position foreach (var error in _errorRanges) { if (error.Contains(pointer)) { // get context menu if (ContextMenu == null) { ContextMenu = new ContextMenu(); } var ctx = ContextMenu; ctx.Tag = error; // update context menu with suggestions ctx.Items.Clear(); foreach (var suggestion in _spell.GetSuggestions(error.Text)) { var item = new MenuItem(); item.Header = suggestion; item.FontWeight = FontWeights.Bold; item.Click += suggestion_Click; ctx.Items.Add(item); } if (ctx.Items.Count == 0) { var item = new MenuItem(); item.Header = MENU_NOSUGGESTIONS; item.IsEnabled = false; ctx.Items.Add(item); } // update context menu with spell commands ctx.Items.Add(new Separator()); foreach (string s in new string[] { MENU_IGNOREALL, MENU_ADDTODICTIONARY }) { var item = new MenuItem(); item.Header = s; item.Click += item_Click; ctx.Items.Add(item); } // done break; } } // fire event as usual base.OnContextMenuOpening(e); }