Solved

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

Posted on 2009-05-08
1
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now