Note that there are some explanatory texts on larger screens.

plurals
  1. POSpell checker in a WPF RichTextBox
    primarykey
    data
    text
    <p>I have created a <code>RichTextBox</code> text editor as a c# Form application solution. I have added a WPF application to this solution to act as a spellchecker. Basically, when I run the spell checker the contents of the <code>RichTextBox</code> are copied to a WPF <code>RichTextBox</code> in the WPF application which I have configured to look like a spell check dialog. I have included a <code>ListBox</code> which displays the spelling error suggestions and buttons that allow the user to ignore errors or change if one of the suggestions are clicked.</p> <p>The main routine loops through the text in the RichTextBox until it finds a spelling error. The user then has the option to ignore by clicking a button that calls <code>EditingCommands.IgnoreSpellingError</code> or change by clicking a button that calls <code>EditingCommands.CorrectSpellingError</code>. Currently I am using another button to then re-cycle through the <code>RichTextBox</code> to find the next spelling error.</p> <p>Ideally what I would like to happen is, for example, when the ignore button is clicked, the <code>EditingCommands.IgnoreSpellingError</code> is called and then the main routine is run immediately after, like so:</p> <pre><code> private void button3_Click(object sender, RoutedEventArgs e) { button3.Command = EditingCommands.IgnoreSpellingError; button3.CommandTarget = richTextBox1; spellCheckWords(); } </code></pre> <p>However, this does not work as the <code>RichTextBox</code> seems to go out of synchronisation with the spell checker. It's as if the form or the <code>RichTextBox</code> are not refreshing properly. Just to re-iterate, if I use a separate button to re-run the main routine then it works ok.</p> <p>The full <strong>code</strong> is below.</p> <p>Any help would be appreciated.</p> <pre><code>using System; using System.Threading; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); richTextBox1.SpellCheck.IsEnabled = true; } private void Window_Loaded(object sender, RoutedEventArgs e) { richTextBox1.Paste(); richTextBox1.ContextMenu = GetContextMenu(); spellCheckWords(); } private static int chrPos = 0; private void spellCheckWords() { SpellingError spellingError; TextRange spellErrorRange; TextPointer start_pointer, end_pointer; richTextBox1.ContextMenu = GetContextMenu(); richTextBox1.SelectAll(); int txtLen = richTextBox1.Selection.Text.Length; bool noSpellingErrors = true; ; for (int i = chrPos; i &lt; txtLen; i++) { start_pointer = richTextBox1.Document.ContentStart.GetNextInsertionPosition (LogicalDirection.Forward).GetPositionAtOffset(i, LogicalDirection.Forward); spellingError = richTextBox1.GetSpellingError(start_pointer); if (spellingError != null) { spellErrorRange = richTextBox1.GetSpellingErrorRange(start_pointer); int errRange = spellErrorRange.Text.Length; textBox1.Text = spellErrorRange.Text; noSpellingErrors = true; string textRun = start_pointer.GetTextInRun(LogicalDirection.Forward); string trimmedString = string.Empty; end_pointer = richTextBox1.Document.ContentStart.GetNextInsertionPosition (LogicalDirection.Forward).GetPositionAtOffset(i + errRange, LogicalDirection.Forward); richTextBox1.Selection.Select(start_pointer, start_pointer); richTextBox1.Focus(); Rect screenPos = richTextBox1.Selection.Start.GetCharacterRect(LogicalDirection.Forward); double offset = screenPos.Top + richTextBox1.VerticalOffset; richTextBox1.ScrollToVerticalOffset(offset - richTextBox1.ActualHeight / 2); listBox1.Items.Clear(); foreach (string str in spellingError.Suggestions) { listBox1.Items.Add(str); } //chrPos = i + errRange + 1; return; } } if (noSpellingErrors == true) { textBox1.Text = "No spelling Errors"; listBox1.Items.Clear(); button2.IsEnabled = false; button3.IsEnabled = false; button4.IsEnabled = false; button6.IsEnabled = false; } } private void button1_Click(object sender, RoutedEventArgs e) { richTextBox1.SelectAll(); richTextBox1.Copy(); richTextBox1.Document.Blocks.Clear(); richTextBox1.SpellCheck.IsEnabled = false; this.Close(); } private ContextMenu GetContextMenu() { return null; } private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listBox1.SelectedItem != null) textBox1.Text = listBox1.SelectedItem.ToString(); } private void button2_Click(object sender, RoutedEventArgs e) { button2.Command = EditingCommands.CorrectSpellingError; button2.CommandParameter = textBox1.Text; button2.CommandTarget = richTextBox1; spellCheckWords(); } private void button3_Click(object sender, RoutedEventArgs e) { button3.Command = EditingCommands.IgnoreSpellingError; button3.CommandTarget = richTextBox1; spellCheckWords(); } private void button4_Click(object sender, RoutedEventArgs e) { spellCheckWords(); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload