Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, I found a way how to color the text between tags in RichTextBox using C# WPF, but the problem is that it colors the text only once, no matter how many times the tag is found.

I'll post my code here, would anyone tell me what's wrong with it? Thanks!


In my Window_Loaded function:


TextRange textRange = new TextRange(lesson_content.Document.ContentStart, lesson_content.Document.ContentEnd);

string[] tags = new string[2];
tags[0] = "t";
tags[1] = "blue";


foreach (string tag in tags)
{
   if (tag == "t")
   {
   string s = ExtractString(textRange.Text, tag);

   Regex reg = new Regex(s, RegexOptions.Compiled | RegexOptions.IgnoreCase);

   TextPointer start = lesson_content.Document.ContentStart;
   while (start != null && start.CompareTo(lesson_content.Document.ContentEnd) < 0)
   {
      if start.GetPointerContext(LogicalDirection.Forward)==TextPointerContext.Text)
      {
         var match = reg.Match(start.GetTextInRun(LogicalDirection.Forward));
         var textrange = new TextRange(start.GetPositionAtOffset(match.Index,    LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));

         textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new  SolidColorBrush(Colors.Blue));
      }
      start = start.GetNextContextPosition(LogicalDirection.Forward);
    }
}

ExtractString Function:


string ExtractString(string s, string tag)
{
   string startTag = "<" + tag + ">";
   int startIndex = s.IndexOf(startTag) + startTag.Length;
   int endIndex = s.IndexOf(", startIndex);

   return s.Substring(startIndex, endIndex - startIndex);
}


I tried to put two <t> tags, but it only colors the text between the first ones.
Posted

1 solution

Not clear what you mean by "tag". Hope you don't mix up HTML with Rich Text. Not quite clear what you want to do and why. I only want to note that if you continue coding in this way, it well lead you nowhere.

Assignment of tag can be written like this: string[] tags = new string[] {"t", "blue"}. What you do is not well maintainable. What if you want to add one more tag? You will need to change two places in code, otherwise tags[2] will cause exception.

You use "t" in two different places. What if you change the tag? Are you going to find all places where you use it? By that reasons, you should never use immediate constants at all (except of few such as 0, 1, null). Not even ""; use string.Empty instead.

Expressions like startTag = "<" + tag + ">" are bad. Did you know that strings are immutable? Avoid repeated concatenation. Use string.Format("<{0}>", tag); in other cases use System.Text.StringBuilder. You tend to use ad-hoc coding and accidental complexity. Try to get rid of it.

—SA
 
Share this answer
 
Comments
George Findulov 14-Jul-11 15:25pm    
First of all, thanks for your reply! <t> tags mean that the text between them should have larger size. <blue> tags mean that the text between them should be colored in blue. The algorithm does what it has to do only when it finds one occurrence of the text between tags. It has to find all occurrences and that's the part i can't handle with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900