Get last entered RichTextBox word using C #

How to get the last word entered and its index position (word between two spaces, as soon as I hit the space, I need to get the word) in RichTextBox. I used the following code to get the last word entered and its index position if that word is at 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 can I get the last word entered and its index position, if I am in the middle of a sentence in RTB (for example, “fast fox” is a sentence; if I write “jumps” after “fox”, then using in the above code I can get the last word entered, but if I put the cursor after “fast” and write “brown” after “fast”, how do I get the last word entered (that is, brown) as soon as I press the spacebar.

Please, help

0
c # winforms richtextbox
source share
4 answers

Ok, my first answer, this 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 carriage position. SelectionStart is usually used to find out where a user's text selection begins and ends ("blue selected text"). However, even if there is no choice, it still works to get the current caret position.

Then, to enter the entered word, it goes back until it finds a space (or index 0, for the first word).

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

It works for words everywhere, but it has one weakness: if instead of placing the carriage after “fast” and entering “brown space”, the user places the carriage in “fast” and types “SPACE brown” well, this is impossible to detect currently.

+5
source share

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 check it right now, but it should give you a strong idea on how to do what you want.

EDIT: Sorry, 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; } 
+1
source share

You can take advantage of some Key and flags events:

 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; } } 

It is important to note the order of events:

  • Keydown
  • Keypress
  • SelectionChanged
  • Keyup
+1
source share

If you are looking for the last word entered, and not just the last word in the field, you will need to create a set of words in the field, and then compare what changed when new words were added.

straight from 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()); } 
0
source share

All Articles