Monday, 19 August 2013

Get last entered word of RichTextBox using c#

Get last entered word of RichTextBox using c#

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

No comments:

Post a Comment