?
Solved

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

Posted on 2009-05-08
1
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
zwei earned 1500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

777 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