Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Master / Programmer.

I'm trying to use Split(), after using it, i wanna check if the input on RTB == my point then change font color on RTB. Like this example.

INPUT on RTB : Chelsea is my favorite football club. I like playing football

My point : football.

Then I split the input, then i check the result of split in arr each index.

Finally, found ex : arr[4] and [9] = football

Then, how to change font color on RTB screen like
"Chelsea is my favorite football. I like playing football club."?

This my code example :
C#
ArrayList arrInput = new ArrayList();
//if the input Chelsea is my favorite football club. I like playing football
string allInput = rtbInput.Text;

string[] splitString = allInput.Split(new char[] { ' ', '\t', ',', '.'});

foreach (string s in splitString)
{
    if (s.Trim() != "")
    {
         int selectionStart = s.ToLower().IndexOf("football");

         if (selectionStart != -1)
         {
             rtbInput.SelectionStart = selectionStart;
             rtbInput.SelectionLength = ("football").Length;
             rtbInput.SelectionColor = Color.Red;
         }
}
//What Next?? We know that football on arrInput[4].ToString() and [9]
//How to change font color on RTB screen when the input == football




Posted
Updated 11-Mar-13 3:37am
v4
Comments
johannesnestler 11-Mar-13 9:02am    
I'm just curious - what is a RTB Screen?
Berry Harahap 11-Mar-13 9:36am    
Dear @johannesnestler..
Sorry, it means RichTextBox. :) :) :)
AssemblySoft 11-Mar-13 10:07am    
Berry, is this a WPF, Windows Forms or Asp.Net application? RTB - i guess you are talking about rich text box, need to know what rich text box you are using.
Berry Harahap 11-Mar-13 10:10am    
Dear @Carl Randall
Using C#, Windows Form.
:) :) :)
Sergey Alexandrovich Kryukov 11-Mar-13 10:44am    
You need to tag it all. So far, you have some questions and answers about nothing.
—SA

1 solution

Hi,
There are few things wrong here.

There is an unused variable,
C#
ArrayList arrInput = new ArrayList();

I am not sure why you have this variable for.

The string has been split into words and then foreach word a looking for the word "football" and getting the index of that word
C#
int selectionStart = s.ToLower().IndexOf("football");

This will always return selectionSrart as 0; because you not looking for an index in the whole sentance instead just that word.

So in your code above, only the first 8 characters will be in the colour you set, but what you actual text you wanted wouldn't be in colour.

What you need is:
C#
int index = 0;
while (index != -1)
{
    // on the first round the index will be zero, thereafter the index will not be zero
    // therefore the index needs to be incremented by 1 from the last index so it can continue 
    // searching, otherwise the search will stuck to that same index.
    index = str.IndexOf(rtbInput.Text, (index != 0) ? (index + 1) : index );
    if ( index != -1)
    {
        rtbInput.SelectionStart = index;
        rtbInput.SelectionLength = ("football").Length;
        rtbInput.SelectionColor = Color.Red;
    }
}


I hope this helps.

Regards
Jegan
 
Share this answer
 
v3

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