C#RichTextBox仅在第一个标签之间的文本上色 [英] C# RichTextBox colors only the text between the first tags

查看:131
本文介绍了C#RichTextBox仅在第一个标签之间的文本上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我找到了一种使用C#WPF在RichTextBox中的标签之间为文本着色的方法,但是问题是,无论找到了多少次标签,它都仅对文本着色一次.

我将在此处发布我的代码,有人会告诉我它有什么问题吗?谢谢!


在我的Window_Loaded函数中:


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函数:



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);
}



我试图放两个< t>



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

推荐答案

不清楚"tag"的含义.希望您不要将HTML与RTF混淆.不太清楚您想做什么以及为什么要做.我只想指出,如果您继续以这种方式进行编码,很可能导致您无所适从.

tag的分配可以这样写:string[] tags = new string[] {"t", "blue"}.您所做的工作无法很好地维护.如果您想再添加一个标签怎么办?您将需要在代码中更改两个位置,否则tags[2]会导致异常.

您在两个不同的地方使用"t".如果更改标签怎么办?您要查找所有使用它的地方吗?因此,您绝对不要使用立即常量(除了0、1,null之类的少数常量). ""都不是;请使用string.Empty.

startTag = "<" + tag + ">"这样的表达式是错误的.您知道字符串是immutable吗?避免重复连接.使用string.Format("<{0}>", tag);在其他情况下,请使用System.Text.StringBuilder.您倾向于使用即席编码和偶然的复杂性.尝试摆脱它.

—SA
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


这篇关于C#RichTextBox仅在第一个标签之间的文本上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆