Click here to Skip to main content
15,919,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code:
C#
private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            int i = richTextBox1.Find("int");
            if (i >= 0)
            {
                richTextBox1.Select(i, "int".Length);
                richTextBox1.SelectionColor = Color.Red;

                richTextBox1.Focus();
                richTextBox1.SelectionStart = richTextBox1.Text.Length;

                richTextBox1.ForeColor = Color.Black;
            }
            else
            {
                return;
            }
        }


It gives to me 2 problems:
1. After finding and drawing red color for "int" keyword, it doesn't stop. When i continue typing any key, it still draws red color. I don't know why.

2. My code is only working in line 1. When I press Enter and type "int", the color is black.

Can you tell me how to fix it?
Thanks!
Posted

Search the articles here for "RichTextBox syntax highlighting[^]". There's a ton of examples on how to do it.
 
Share this answer
 
I'll post a code example to help get you started, but keep mind there are a lot of issues this code doesn't handle, like:

1. when you insert 'int' in a line before the last line of the RichTextBox
2. when you type 'int' at the start of the first line.

To really make any kind of robust word-coloring function, you are going to need to keep track of the words you have colored using some kind of data structure that records their location; that gets tricky because their location may change any time the user adds new content, or deletes old content, back-spaces, etc.

Think about what would happen if you set the forecolor of an occurrence of "int" to red, and then the user edited the text so that "int" was changed to "in:" clearly, you should be setting the forecolor of "in" back to your default text-color. You can't do that unless you maintain some kind of structure that keeps track of all instances of "int" !

So, do take Dave's advice and study the examples here on CodeProject.

This sample is meant just to give you the idea of saving state (current selection start and selection color), and restoring it after you color a word:
C#
private int currentSelectionIndex;
private Color currentSelectionColor;
private string stringToFind = " int ";

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    int i = richTextBox1.Text.LastIndexOf(stringToFind);

    if (i >= 0)
    {
        currentSelectionIndex = richTextBox1.SelectionStart;
        currentSelectionColor = richTextBox1.SelectionColor;

        richTextBox1.Select(i, stringToFind.Length);
        richTextBox1.SelectionColor = Color.Red;

        richTextBox1.SelectionStart = currentSelectionIndex;
        richTextBox1.SelectionColor = currentSelectionColor;
    }
}
 
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