2

我在我的 WPF Richtextbox 上启用了拼写,我想在显示带有拼写建议的上下文菜单之前在当前插入符号位置获取拼写错误的单词。

4

4 回答 4

4

新方式

    void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back)
        {
            TextPointer start = richTextBox1.CaretPosition;
            string text1 = start.GetTextInRun(LogicalDirection.Backward);
            TextPointer end = start.GetNextContextPosition(LogicalDirection.Backward);
            string text2 = end.GetTextInRun(LogicalDirection.Backward);

            richTextBox1.Selection.Select(start, end);
            richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
            richTextBox1.Selection.Select(start, start);
            //e.Handled = true;
        }
    }
于 2011-04-21T08:21:52.803 回答
1

看看这个 http://www.dotnetfunda.com/articles/article842-spellchecker-in-wpf-.aspx

就在这里似乎讨论了一些可能有助于您的方案的选项:“这里我们使用 SpellingError 类来获取建议。CaretIndex 返回克拉在文本框中的索引。GetSpellingError 只能在当前克拉位置有时返回 SpellingError 对象一个有错误的单词,并且为 TextBox 启用了拼写检查。"

于 2010-10-14T16:58:20.440 回答
1

老办法

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
        {
            var z = this.richTextBox1.SelectionStart;
            var r = richTextBox1.Find(" ", 0, z, RichTextBoxFinds.None | RichTextBoxFinds.Reverse);
            var q = this.richTextBox1.Text.Substring(r + 1, z - r - 1);
            switch (q)
            {
                case "test":
                    this.richTextBox1.SelectionStart = r + 1;
                    this.richTextBox1.SelectionLength = z - r - 1;
                    this.richTextBox1.SelectionColor = Color.Black;
                    this.richTextBox1.SelectionStart += this.richTextBox1.SelectionLength;
                    this.richTextBox1.SelectionLength = 0;
                    //e.Handled = true;
                    break;
                default:
                    this.richTextBox1.SelectionStart = z;
                    break;
            }
        }
    }
于 2011-04-21T07:20:32.940 回答
1

备查:

void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
   var rtb = (RichTextBox)sender;
   var tr = rtb.GetSpellingErrorRange(rtb.CaretPosition);
   if(tr != null)
   {
       string spellingerror = tr.Text;
       //Do whatever
   }
}
于 2015-02-22T07:21:04.547 回答