RichTextBox选择par文本

时间:2012-12-29 20:56:33

标签: c# textbox

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

1 个答案:

答案 0 :(得分:2)

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