[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-05-08
1
Medium Priority
?
228 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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