Get last entered word of RichTextBox using c#

别来无恙 提交于 2019-12-13 04:34:07

问题


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


回答1:


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.




回答2:


You can try something like this

    RichTextBox richTextBox = new RichTextBox();
    string lastWord;
    private void KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == ' ')
        {
            lastWord = richTextBox.Text.Split(' ')[richTextBox.Text.Split(' ').Count() - 2];
        }
    }

I can't exactly test it right now, but it should give you a strong idea on how to do what you want.

EDIT::: Sorry about that, try this instead

    string lastword;
    private void KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == ' ')
        {
            lastword = ReadBack(rtb.SelectionStart);
        }
    }
    private string ReadBack(int start)
    {
        string builder = "";
        for (int x = start; x >= 0; x--)
        {
            Char c = rtb.Text[x];
            if (c == ' ')
                break;
            builder += c;
        }
        Array.Reverse(builder.ToArray());
        return builder;
    }



回答3:


You can benefit from some Key events and flags:

bool newStart; 
int startIndex;
//SelectionChanged event handler for richTextBox1
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
     //record the new SelectionStart
     if (newStart) startIndex = richTextBox1.SelectionStart;                        
}
//KeyDown event handler for richTextBox1
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
     newStart = char.IsControl((char)e.KeyCode) ||
                         ((int)e.KeyCode < 41 && (int)e.KeyCode > 36);
}
//KeyUp event handler for richTextBox1
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
     newStart = true;//This makes sure if we change the Selection by mouse
     //the new SelectionStart will be recorded.
}
//KeyPress event handler for richTextBox1
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == ' '){
      MessageBox.Show(richTextBox1.Text.Substring(startIndex, richTextBox1.SelectionStart - startIndex));          
      newStart = true;
   }                   
}

The important thing to notice here is the order of the events:

  1. KeyDown
  2. KeyPress
  3. SelectionChanged
  4. KeyUp



回答4:


If you are looking for the last entered word, not just the last word in the box, you are going to need to create a collection of words in the box, then compare what has changed when new words are added.

just off the top of my head

    private string oldwords = "";
    private string newwords = "";
    private string lastword;
    private int index;

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar != ' ') return;
      findLastword();
    }

    private void findLastword()
    {
      newwords = richTextBox1.Text;
      lastword = newwords.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                          .Except(oldwords.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                          .ToList().FirstOrDefault() ?? "";

      oldwords = newwords;
      index = newwords.IndexOf(lastword.FirstOrDefault());
    }


来源:https://stackoverflow.com/questions/18320521/get-last-entered-word-of-richtextbox-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!