C# 我怎样才能得到我的;替换“文本”;代码工作?

C# 我怎样才能得到我的;替换“文本”;代码工作?,c#,.net,winforms,C#,.net,Winforms,我在应用程序中使用NetSpell是为了提供拼写检查。除NetSpell的一个功能外,所有功能都正常工作。这就是“替换单词”功能(拼写检查器中的一个重要功能,嗯?) 代码如下: private NetSpell.SpellChecker.Spelling spelling; private NetSpell.SpellChecker.Dictionary.WordDictionary wordDictionary; internal System.Windows.Forms.Bu

我在应用程序中使用NetSpell是为了提供拼写检查。除NetSpell的一个功能外,所有功能都正常工作。这就是“替换单词”功能(拼写检查器中的一个重要功能,嗯?)

代码如下:

private NetSpell.SpellChecker.Spelling spelling;
    private NetSpell.SpellChecker.Dictionary.WordDictionary wordDictionary;
    internal System.Windows.Forms.Button spellButton;
    internal System.Windows.Forms.RichTextBox demoRichText;
    private System.ComponentModel.IContainer components2;
    internal System.Windows.Forms.RichTextBox Document;
    internal NetSpell.SpellChecker.Spelling SpellChecker;
    private System.ComponentModel.IContainer components1;
    internal NetSpell.SpellChecker.Dictionary.WordDictionary WordDictionary;
启动拼写检查:

 private void toolStripButton1_Click(object sender, EventArgs e)
    {
        try
        {
            if (richTextBoxPrintCtrl1.Text == null)
            {
                MessageBox.Show("There is no text to check. Spellcheck will not be launched", "No Text", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                this.spelling1.Text = this.richTextBoxPrintCtrl1.Text;
                this.spelling1.SpellCheck();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error loading Spell Checker. Please reload application and try again. " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
替换单词:

 private void spelling_ReplacedWord(object sender, NetSpell.SpellChecker.ReplaceWordEventArgs e)
        {
            int start = this.richTextBoxPrintCtrl1.SelectionStart;
            int length = this.richTextBoxPrintCtrl1.SelectionLength;

            this.richTextBoxPrintCtrl1.Select(e.TextIndex, e.Word.Length);
            this.richTextBoxPrintCtrl1.SelectedText = e.ReplacementWord;

            if (start > this.richTextBoxPrintCtrl1.Text.Length)
                start = this.richTextBoxPrintCtrl1.Text.Length;

            if ((start + length) > this.richTextBoxPrintCtrl1.Text.Length)
                length = 0;

            this.richTextBoxPrintCtrl1.Select(start, length);
        }
删除单词:

private void spelling_DeletedWord(object sender, NetSpell.SpellChecker.SpellingEventArgs e)
    {
        int start = this.richTextBoxPrintCtrl1.SelectionStart;
        int length = this.richTextBoxPrintCtrl1.SelectionLength;

        this.richTextBoxPrintCtrl1.Select(e.TextIndex, e.Word.Length);
        this.richTextBoxPrintCtrl1.SelectedText = "";

        if (start > this.richTextBoxPrintCtrl1.Text.Length)
            start = this.richTextBoxPrintCtrl1.Text.Length;

        if ((start + length) > this.richTextBoxPrintCtrl1.Text.Length)
            length = 0;

        this.richTextBoxPrintCtrl1.Select(start, length);
    }
我现在不知道问题在哪里。有人能帮我指出正确的方向吗

谢谢

--编辑--

我已经添加了所有与拼写检查功能一起使用的代码。如果这是错误的,我很抱歉,但我是编程新手,我仍然不完全理解所有的技术术语,所以我只是尽我所能帮助您帮助我o)

--编辑2--

我发现了更多。“全部替换”功能在一定程度上起作用。如果我更改一个单词并按“全部替换”,它会在拼写检查中更改它,但不会在表单上更改它。例如,请参见以下图片


我自己创建了这两个函数,并对它们进行了测试。他们正在工作。您可以从事件处理程序调用它们

static public class RichTextBoxExtensions
{
    static public void ReplaceWord(this RichTextBox rtb, int index, int length, string newWord)
    {
        rtb.Select(index, length);
        rtb.SelectedText = newWord;
        rtb.Select(index, newWord.Length);
    }

    static public void ReplaceWord(this RichTextBox rtb, string oldWord, string newWord)
    {
        rtb.ReplaceWord(rtb.Text.IndexOf(oldWord), oldWord.Length, newWord);
    }

}

你没有说问题是什么…我说了。我说过“替换单词”功能不起作用。这意味着当用户单击“替换单词”时,单词不会被替换…是的,但这与用户说“程序不工作!!!恐慌症发作”是一样的。在这里问一个问题主要是关于这一点是行不通的。请尝试解释什么不起作用。文本根本没有被修改吗?文本被打乱了吗?这个函数应该做什么?只替换一个单词还是多个单词?如果您通过调试器运行它,会发生什么?开始和长度是否与预期的一致?哦。。很抱歉不,文本不会在富文本框中被修改。你能给出一个简单的例子(在你的回答中)你的文本框的内容,以及事件处理程序中的开始+长度+重放词吗?文本框的内容是可变的(该程序是一个字处理器)。我理解这一点,但我要求一个简单的例子,它在其中不起作用,使用这些变量。因为我开始担心您的逻辑可能没有问题,但是
e
中的变量不正确。我将为拼写检查器发布整个代码。(对不起,我是编程新手,所以我还在学习所有的术语。):哦。。。两种方法都是好方法。通过手工或设计师。但我很高兴它现在起作用了