开发者

Changing the text.ForeColor of all matched instances using Regex in C#

开发者 https://www.devze.com 2023-03-02 16:57 出处:网络
I have a multi-lined TextBox. I have also an Input box used in specifying a Regex string that is used in formatting the text that is entered in the TextBox.

I have a multi-lined TextBox. I have also an Input box used in specifying a Regex string that is used in formatting the text that is entered in the TextBox.

Regex.Replace replaces strings. What I want is to apply some styling to the found matched as using Bold, Italic and ch开发者_如何学Pythonange its ForeColor.

For Example:

Text in TextBox:

Change all text in the multi-lined text box that matches the text inside the input box's text box

String in Input Box/Regex:

text

And I have:

RegexOptions.IgnoreCase

The desired result:

Change all text in the multi-lined text box that matches the text inside the input box's text box


You need to use a RichTextBox for this. First use the RegEx to find the indexes of the sub strings and then use SelectionStart and SelectionEnd to select that text in the RTB and SelectionFont and SelectionColor to make it bold and colored.

0

精彩评论

暂无评论...
验证码 换一张
取 消