Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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