Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 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