【问题标题】:RichTextBox select par of text [closed]RichTextBox 选择文本部分 [关闭]
【发布时间】:2012-12-30 05:53:44
【问题描述】:

我有RichTextBox。如何在 /**/ 之间选择文本并将此文本设为绿色?

【问题讨论】:

  • 您是想自己想一想,还是要在这里问一切?你试过什么?
  • @sa_ddam213 这是他自己的问题
  • RichTextbox 的内容是FlowDocument。您可能想从这里开始,并使用此MSDN Forum Post
  • 既然你没有以你不想做的方式提供任何信息,这个小片段代码会给你一个想法;这是在 WPF string searchFor = "/*" + textBox4.Text + "*/"; var finalvalue = searchFor.Trim( new Char[] { '/', '*' } ); textBox4.Foreground = Brushes.Green;

标签: c# textbox


【解决方案1】:
string text = "abc /*defg*/ hij /*klm*/ xyz";
richTextBox1.Text = text;

Regex.Matches(text, @"\/\*(.*?)\*\/",RegexOptions.Singleline).Cast<Match>()
        .ToList()
        .ForEach(m =>
        {
            richTextBox1.Select(m.Index, m.Value.Length);
            richTextBox1.SelectionColor = Color.Blue;
            //or 
            //richTextBox1.Select(m.Groups[1].Index, m.Groups[1].Value.Length);
            //richTextBox1.SelectionColor = Color.Blue;
        });

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 2010-11-10
    • 1970-01-01
    • 2012-08-19
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    相关资源
    最近更新 更多