I have RichTextBox
. How I can select there text between /*
and */
and make this text green color?
Asked
Active
Viewed 207 times
2

Andrii
- 131
- 1
- 2
- 7
-
1Possible duplicate: http://stackoverflow.com/questions/14085607/textbox-text-color-change – sa_ddam213 Dec 29 '12 at 20:58
-
2Do you want to think about something yourself or are you going to ask everything here? What have you tried? – Vultour Dec 29 '12 at 20:58
-
1@sa_ddam213 thats his own question – Vultour Dec 29 '12 at 20:59
-
The content of the RichTextbox is a [`FlowDocument`](http://msdn.microsoft.com/en-us/library/system.windows.documents.flowdocument.aspx). You may want to start there and with this [MSDN Forum Post](http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/01c9faac-2c37-40ca-9ca5-91d433033671/) – Mark Hall Dec 29 '12 at 21:13
-
Since, you haven't given any info in the way you wan't to do it, this small piece code would give you an idea; This is in WPF `string searchFor = "/*" + textBox4.Text + "*/"; var finalvalue = searchFor.Trim( new Char[] { '/', '*' } ); textBox4.Foreground = Brushes.Green;` – linguini Dec 29 '12 at 21:36
-
http://stackoverflow.com/questions/5442067/change-color-and-font-for-some-part-of-text-in-wpf-c-sharp – linguini Dec 29 '12 at 21:37
1 Answers
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;
});

I4V
- 34,891
- 6
- 67
- 79