Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want on loading richtextbox file the alphabets color are different for different alphabets.

like a is green, b is white, c is red etc..
Posted

you can try to change color through "EditedControlShowind event"
 
Share this answer
 
You could do this by selecting each character in the RichTextBox, one-at-a-time, and calling YourRichTextBox.SelectionColor = #SomeColor;

Where you vary #SomeColor using a function to calculate, or look-up, a new color for each character.

However, if you do this, in a RichTextBox with a lot of text, what you will end up with is (very likely) a flickering mess, even if you use some "ancient" technique for reducing RichTexBox flicker like this: [^].

Assuming you have some special purpose in mind, one way you might approach this ... with speed in mind ... is to create a look-up table of characters and colors. That's assuming you always want the same character to be the same color.

You could create a Dictionary like this:
C#
private Dictionary<char, Color> dctCharColor;
Then initialize it somewhere in your code:
C#
dctCharColor = new Dictionary<char, Color>();
{
     { 'a', Color.Green },
     { 'b', Color.White},
     { 'c', Color.Red }
};
And then, for example, you could color the characters with a method like this:
C#
private void ApplyColor(RichTextBox theRichTextBox)
{
    theRichTextBox.SuspendLayout();
    
    for (int i = 0; i < theRichTextBox.TextLength; i++)
    {
        ch = theRichTextBox.Text.ToLower()[i];

        if (dctCharColor.Keys.Contains(ch))
        {
            theRichTextBox.Select(i, 1);
            theRichTextBox.SelectionColor = dctCharColor[ch];
        }
    }
    
    theRichTextBox.ResumeLayout();
}
I would make the choice to set the character colors after the RichTextBox was filled with its content because I'd want to "isolate" file reading and handle any errors in file reading separately.
 
Share this answer
 
v2
Comments
Dharmendra-18 25-Nov-13 7:08am    
this is not work its create hanging problem..
any other..

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