Solved

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

Posted on 2009-05-08
1
221 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:MSFanboy
1 Comment
 
LVL 7

Accepted Solution

by:
zwei earned 500 total points
ID: 24343814
How about this:
    private Regex keyWords = new Regex("AND|OR");

        

    private void rowfilterRichTextBox_TextChanged(object sender, EventArgs e)

    {

        int pos = rowfilterRichTextBox.SelectionStart;

            

        rowfilterRichTextBox.SelectAll();

        rowfilterRichTextBox.SelectionColor = Color.Black;

            

        int selPos = rowfilterRichTextBox.SelectionStart;

        foreach (Match keyWordMatch in keyWords.Matches(rowfilterRichTextBox.Text))

        {

            rowfilterRichTextBox.Select(keyWordMatch.Index, keyWordMatch.Length);

            rowfilterRichTextBox.SelectionColor = Color.Blue;

        }

        

        rowfilterRichTextBox.DeselectAll();

        rowfilterRichTextBox.SelectionStart = pos;

    }

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now