How to extract last word in richtextbox in c#
Asked Answered
S

3

-3

I am working with windows form. In my project i need to color the last word of rich text box. When someone write on the given text box of the application i need the last word that is just written on the richtextbox to be colored red or whatever.

I found a way to extract the last word from the following link

http://msdn.microsoft.com/en-us/library/system.windows.documents.textselection.select%28v=vs.95%29.aspx

But i need more handy code to extract the last word if its possible. Please help.

Sugden answered 14/9, 2012 at 20:33 Comment(2)
The code you supplied seems very straightforward.Levesque
Have you tried that code? Looks good. What needs to be better about it?Illiquid
N
1

Here is the sample code

*> char[] arr = new char[50];

      int i = 0;
    private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
     {
    if (e.KeyCode == Keys.Space)            {
        string str = new string(arr); 
        MessageBox.Show(str);
        Array.Clear(arr, 0, arr.Length);
        i = 0;
    }
    else if (e.KeyCode == Keys.Back)
       {
        i--;
        if (i < 0)
        {
            i = 0;
        }
        arr[i] = ' ';


    }

    else
    {
        arr[i] = (char)e.KeyValue;

        i++;
    }
    }*

This is how you will be able to extract the latest word. Now color yourself the word you like.

Novocaine answered 14/9, 2012 at 22:42 Comment(0)
R
2

Well, if you really are just looking to get the last word, you could do something like this...Assuming of course, that you make a string equal to the text of your rich text box.

string str="hello, how are you doing?";
if (str.Length >0)
{
    int index=str.LastIndexOf(" ") + 1;
    str = str.Substring(index));
}

Then just return the string, and do what you need to do with it.

Reliable answered 14/9, 2012 at 20:57 Comment(2)
This won't help him color that word, and also won't work if the last word is in a paragraph of its own.Levesque
how does this have anything to do with a rich text box in c#Plasmosome
N
1

Here is the sample code

*> char[] arr = new char[50];

      int i = 0;
    private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
     {
    if (e.KeyCode == Keys.Space)            {
        string str = new string(arr); 
        MessageBox.Show(str);
        Array.Clear(arr, 0, arr.Length);
        i = 0;
    }
    else if (e.KeyCode == Keys.Back)
       {
        i--;
        if (i < 0)
        {
            i = 0;
        }
        arr[i] = ' ';


    }

    else
    {
        arr[i] = (char)e.KeyValue;

        i++;
    }
    }*

This is how you will be able to extract the latest word. Now color yourself the word you like.

Novocaine answered 14/9, 2012 at 22:42 Comment(0)
M
-1

As I remember rich text box can render text as HTML.
Just wrap your last word in font tag and that's it.

Masonmasonic answered 14/9, 2012 at 23:58 Comment(1)
extract the last word is the question, not how to color a wordPlasmosome

© 2022 - 2024 — McMap. All rights reserved.