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.
Pierre-Luc Pineault
source share