开发者

remove back color from multiple positions in richTextBox

I have a rich text box and I've implemented a search option for it. When the user searches a string, all the matches gets highlighted with yellow background. I want that when the user presses the search button again, all of the prev开发者_StackOverflow中文版ious highlights will be removed before the new search begins.

I found out two ways to do it: 1. select all text and then choose the back color to be the default one. 2. remove all text from the text box and then put it back again.

Both ways work, but it doesn't look natural when I use them. So, is there another way that I can remove all highlights from the text?

I'm using .NET framework 4 and I write in C#.


Try this code:

richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = System.Drawing.Color.White;
richTextBox1.DeselectAll();

here White will be the backcolor of Text before it gets highlighted with yellow color


The functionality that you're looking for is the multiple selection, something like:

richTextBox1.Select(4, 5);
richTextBox1.Select(29, 2);
richTextBox1.Select(95, 12);

but still have the previous selections selected.

Bad news because multiple selection is not build-in function in richTextBox, but I think you may do some tricks to achieve this:

Select one part make the selection highlighted (later make it normal when unselect) and record the part beginning & ending index and the same with second and third and more...

Hope it helps


There is a very simple solution to removing multiple instances of highlights that you have created without interfering with all other highlights, other formatting etc:

Use a unique highlight color no-one else is likely to be using eg

hColor as color = Color.FromArgb(255, 255, 1)

Then to remove all instances of highlights in that color from your richtext use:

Dim t As String = TextBox1.Rtf
t = t.Replace("\red255\green255\blue1;", "\red255\green255\blue255;")
TextBox1.Rtf = t

This replaces your special highlight color with the same color as the background, in this case Color.FromArgb(255, 255, 255), without having to search for any highlighted words or implement any other code.

Bye bye highlight...


Another solution is to take the RTF string from the RichTextBox's RTF property and use Regex to replace the Color Table and Highlight tags. You can then take the stripped string and use it in the RichTextBox. Hope this simple helper method helps someone...

   public string StripRTFColor (string RTFString)
    {
        string result = "";

        //                      
        //STRIP COLOUR TABLES   
        //                      

        string regexSearchString = @"\{\\colortb.*\}\r\n";

        result = Regex.Replace(RTFString, regexSearchString, "");


        //                      
        //STRIP HIGHLIGHT TAG   
        //                      
        regexSearchString = @"\\highlight[\d]* ";

        result = Regex.Replace(result, regexSearchString, "");



        return result;
    }


here is the idea written in semi C# psedocode hope it helps

   List<Match> matches = new List<Match> { };


 void Highlight(string SearchString,Color highlightColor)
 {
 foreach (var match in matches)
 {
 UpdateMatchBgColor(richTextBox1,match.pos,match.length,match.color);
 }
 matches = SearchMatches(SearchString);

 foreach (var match in matches)
 {
 UpdateMatchBgColor(richTextBox1,match.pos,match.length,highlightColor);
 }

 }

EDIT: trying this: http://www.dotnetcurry.com/ShowArticle.aspx?ID=146 Edit2:

works awesome!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜