Link to home
Start Free TrialLog in
Avatar of MSFanboy
MSFanboyFlag for Germany

asked on

Handling colorized Text in RichTextBox is buggy moving back with the cursor

Hello,

if the user enters a filter string in my RichTextBox I want the string OR + AND to be colorized.

Below code works until the user goes back with the cursor in order to delete/change code. Then some chars after the cursor gets automatically selected and overwritten, what I do not want. How to change this behaviour, any idea?
public Regex keyWords = new Regex("AND|OR");
 
        private void rowfilterRichTextBox_TextChanged(object sender, EventArgs e)
        {
            int selPos = rowfilterRichTextBox.SelectionStart;
            foreach (Match keyWordMatch in keyWords.Matches(rowfilterRichTextBox.Text))
            {
                rowfilterRichTextBox.Select(keyWordMatch.Index, keyWordMatch.Length);
                rowfilterRichTextBox.SelectionColor = Color.Blue;
                rowfilterRichTextBox.SelectionStart = selPos;
                rowfilterRichTextBox.SelectionColor = Color.Black;
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zwei
zwei
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial