使用c#获取最后输入的RichTextBox单词 [英] Get last entered word of RichTextBox using c#

查看:62
本文介绍了使用c#获取最后输入的RichTextBox单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 RichTextBox 中获取最后输入的单词及其索引位置(两个空格之间的单词.只要按空格,我就需要获取该单词).如果单词位于 RichTextBox 文档的末尾,我已使用以下代码获取最后输入的单词及其索引位置.

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e){if(e.KeyChar == ' '){int i = richTextBox.Text.TrimEnd().LastIndexOf(' ');if(i != -1) MessageBox.Show(richTextBox.Text.Substring(i+1).TrimEnd());}}

但是,如果我在 RTB 中的句子中间输入,我如何获得最后输入的单词及其索引位置(例如,'quick fox' 是句子;如果我写 'jumps' after 'fox' 然后使用上面的代码我可以获得最后输入的单词.但是如果我将光标定位在 'quick' 之后并且在quick"之后写brown",我如何在按下空格键后立即获得最后输入的单词(即brown).>

请帮忙

解决方案

好吧,我的第一个答案,应该更好:

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e){if (e.KeyChar == ' '){int wordEndPosition = richTextBox1.SelectionStart;int currentPosition = wordEndPosition;while (currentPosition > 0 && richTextBox1.Text[currentPosition - 1] != ' '){当前位置 - ;}string word = richTextBox1.Text.Substring(currentPosition, wordEndPosition - currentPosition);}}

SelectionStart 获取当前插入符号位置.SelectionStart 通常用于了解用户的文本选择(蓝色突出显示的文本")的开始和结束位置.但是,即使没有选择,它仍然可以获取当前插入符号位置.

然后,为了获得输入的单词,它会向后移动,直到找到一个空格(或索引 0,对于第一个单词).

所以你用一个小Substring得到这个词,而currentPosition保存你的词的索引.

适用于任何地方的单词,但它有一个弱点:如果用户不是将插入符放在quick"之后并输入brown SPACE"而是将插入符放在quick"并键入SPACE brown",那么这是不可能的当前检测它.

How do I get the last entered word and its index position( word between two spaces. as soon as I press space I need to get the word) within a RichTextBox. I have used the following code to get the last entered word and its index position if the word is in the end of the RichTextBox document.

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e){
         if(e.KeyChar == ' '){
         int i = richTextBox.Text.TrimEnd().LastIndexOf(' ');
        if(i != -1)  MessageBox.Show(richTextBox.Text.Substring(i+1).TrimEnd());
                }
      }

But how do I get the last entered word and its index position if I type in the middle of a sentence in the RTB( e.g. 'quick fox' is the sentence; If I write 'jumps' after 'fox' then using the above code I can get the last entered word. But if I position the cursor after 'quick' and write 'brown' after 'quick' how do I get the last entered word (i.e. brown) as soon as I press space.

Please help

解决方案

Okay screw my first answer, that should be better :

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == ' ')
    {
        int wordEndPosition = richTextBox1.SelectionStart;
        int currentPosition = wordEndPosition;

        while (currentPosition > 0 && richTextBox1.Text[currentPosition - 1] != ' ')
        {
            currentPosition--;
        }

        string word = richTextBox1.Text.Substring(currentPosition, wordEndPosition - currentPosition);
    }
}

SelectionStart gets the current caret position. SelectionStart is normally used to know where the user's text selection (the 'blue highlighted text') starts and ends. However, even when there's no selection, it still works to get the current caret position.

Then, to get the word entered, it goess backwards until it finds a space (or index 0, for the first word).

So you get the word with a little Substring, and currentPosition holds the index of your word.

Works for words everywhere, but it have one weakness : If instead of putting the caret after 'quick ' and entering "brown SPACE" the user places the caret at 'quick' and types "SPACE brown" well, it's not possible to detect it currently.

这篇关于使用c#获取最后输入的RichTextBox单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆