2

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

jeff
  • 684
  • 1
  • 11
  • 30
  • possible duplicate of [How to extract last word in richtextbox in c#](http://stackoverflow.com/questions/12431607/how-to-extract-last-word-in-richtextbox-in-c-sharp) – gunr2171 Aug 19 '13 at 18:48
  • let me see that link if it works in my problem – jeff Aug 19 '13 at 18:56

4 Answers4

6

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.

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
1

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;
    }
ismellike
  • 629
  • 1
  • 5
  • 14
  • You ran into the same issue I did. It's not the last word _in the entire text_, but he needs the last word _that was last typed_. This means if you start typing in the middle of the string, it would pick up there, not the end. I assume you have to do something with the position of the cursor to get this to work. – gunr2171 Aug 19 '13 at 18:46
  • exactly that is what is needed here – jeff Aug 19 '13 at 18:47
  • Yes , this is a better answer – jeff Aug 19 '13 at 19:22
1

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
King King
  • 61,710
  • 16
  • 105
  • 130
0

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());
    }
trashrobber
  • 727
  • 2
  • 9
  • 26