1

RichTextBoxac# Form アプリケーション ソリューションとしてテキスト エディターを作成しました。スペルチェッカーとして機能するように、このソリューションに WPF アプリケーションを追加しました。基本的に、スペル チェッカーを実行すると、スペル チェック ダイアログのように構成した WPF アプリケーションのRichTextBoxWPF に内容がコピーされます。スペル ミスの候補を表示する と、ユーザーがエラーを無視したり、候補の 1 つがクリックされた場合に変更したりできるボタンをRichTextBox含めました。ListBox

メイン ルーチンは、スペル ミスが見つかるまで、RichTextBox 内のテキストをループ処理します。ユーザーは、 を呼び出すボタンをクリックして無視するか、 を呼び出すEditingCommands.IgnoreSpellingErrorボタンをクリックして変更するかを選択できますEditingCommands.CorrectSpellingError。現在、別のボタンを使用してRichTextBox、次のスペルミスを見つけるために繰り返し使用しています。

理想的には、たとえば、無視ボタンがクリックされたときにEditingCommands.IgnoreSpellingErrorが呼び出され、その直後にメイン ルーチンが実行されるようにしたいと考えています。

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        button3.Command = EditingCommands.IgnoreSpellingError;
        button3.CommandTarget = richTextBox1;
        spellCheckWords();
    }

ただし、RichTextBoxがスペル チェッカーと同期していないように見えるため、これは機能しません。フォームまたはRichTextBoxが適切に更新されていないかのようです。繰り返しますが、別のボタンを使用してメイン ルーチンを再実行すると、問題なく動作します。

完全なコードは以下のとおりです。

どんな助けでも大歓迎です。

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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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 < 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();
    }


}

}

4

1 に答える 1