Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to change the font color by value from the variable. If rich text is "hi", the color of the word "hi" is converted to red. If it is "me", the color of the word "you" is blue. In other words, the font color is black. Same as C# to find the word 'if'->'if' color is blue.

The following code works very well:
C#
leng = richTextBox1.Text.Length;
txtL.Text = leng.ToString();
if (leng > 2)
{

     leng = leng - 2;
     //Filter 2 char.
     textBox1.Text = richTextBox1.Text.Substring(leng, 2);
     fchar = richTextBox1.Text.Substring(leng, 2);
     leng = richTextBox1.Text.Length;
}
if (fchar == "hi")
{
     richTextBox1.SelectionStart = richTextBox1.Text.Length - 2;
     richTextBox1.SelectionLength = 2;
     richTextBox1.SelectionColor = Color.Red;
     richTextBox1.SelectionStart = richTextBox1.Text.Length;
     richTextBox1.SelectionLength = 1;
     richTextBox1.SelectionColor = Color.Black;
}
if (fchar == "me")
{
     richTextBox1.SelectionStart = richTextBox1.Text.Length - 2;
     richTextBox1.SelectionLength = 2;
     richTextBox1.SelectionColor = Color.Blue;
     richTextBox1.SelectionStart = richTextBox1.Text.Length;
     richTextBox1.SelectionLength = 1;
     richTextBox1.SelectionColor = Color.Black;
}

But there is a problem is when I add characters to the back or front hi words.
for example: hi-> hii
color of the text is red
I want to add characters at the beginning or end of words hi-> hi there will be black and the characters are added in black
how to do this?
Posted
Updated 2-Oct-11 0:21am
v2
Comments
Bala Selvanayagam 2-Oct-11 5:09am    
Your requirement is not clear
1.If the word is "Me" what color ?
2.If the word is "Hi" what is the colour
3.If the word is "Hii" how do you want the colouring, do you needs the first two letters in one colour and the last char (i) in a different colour
4.What is the colour if some one types an entirly different word Eg: Hello ?
BillWoodruff 2-Oct-11 18:26pm    
Are you looking for the words "hi," and "me" as words: i.e., you are looking for two letters preceded by a space, and followed by a space; or, are you searching for any occurrence of those two letters even if they start, end, or are in the middle of a longer word ?
shakil0304003 2-Oct-11 23:20pm    
Not Clear!
Anuja Pawar Indore 3-Oct-11 2:44am    
Frame your question again and which word you want in which color specify them using quotes, then i think will be clear what you are trying to ask

You can see this link
Click
 
Share this answer
 
thanks.your solution.I am very grateful
I found Regex.It solved my problem.Here's the code:

C#
  //found keyword 'if' or 'while' or '$'
for(Match match4 = new Regex("if | while |$*").Match(richTextBox1.Text); match4.Success; match4 = match4.NextMatch ())
{
             richTextBox1.Select (match4.Index, match4.Value.Length);
             richTextBox1.SelectionColor = Color.Blue;
             richTextBox1.DeselectAll ();
             richTextBox1.SelectionColor = Color.Black;
}
             //after select colors for character-> select black for the next character
             richTextBox1.SelectionStart = richTextBox1.Text.Length;
             richTextBox1.SelectionColor = Color.Black;

You can see the Match match4.Yes,each one containing a keyword match.Code work very well when it found if or while.But only $ is blue while the characters behind the '$' is black
I have seen using Regex.
Second problem is if I'm in line 3,I want to move up the line 2 to edit the text.
Starting position of the new characters are always the last line in rich text.
 
Share this answer
 
v2

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