Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.
I have a RichTextBox which User can type lots of words in that component.
I have a string.When the user is typing a word,i want to save that word in that string and when the user press space in order to write a new word,this string`s value should be changed to null and agian when the user start to write a new word, this string`s value should be fill with this new word and this process will be continue until the end of typing.
What can i do?would you please help me?
Best
Posted
Comments
OriginalGriff 24-Jul-11 4:32am    
That does seem a bit strange: what are you trying to do? You can't do it just on keystrokes (what if the user moves the caret via the mouse? Or selects a couple of lines then types?
Use the "Improve question" widget to edit your question and try to explain what you are trying to achieve, rather than how t=you are trying to do it.

C#
int prevInd = 0;
string lastWord = "";
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    if (richTextBox1.Text.Length == 0)
        return;
    if (richTextBox1.Text[richTextBox1.Text.Length - 1] == ' ')
    {
        lastWord = richTextBox1.Text.Substring(prevInd, richTextBox1.Text.Length - prevInd);
        prevInd += lastWord.Length;
        MessageBox.Show(lastWord);
    }
}
 
Share this answer
 
Comments
k5_ce 24-Jul-11 7:46am    
Thanks alot.It really helped me.
Regards
alrosan 24-Jul-11 9:22am    
you are welecome
Another way,

C#
class Program
{
    static string GetLastWord(string dataToTest)
    {
        return String.IsNullOrEmpty(dataToTest) ? default(string) : dataToTest.Split(new char[] { ' ' }).LastOrDefault();
    }

    static void Main(string[] args)
    {

        string dataToTest = "Hello world I am a String!";
        string lastWord = GetLastWord(dataToTest);
    }
}


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900