Selectively coloring text in RichTextBox

后端 未结 4 1822
深忆病人
深忆病人 2020-11-28 14:50

How can I paint in red every time I meet the letter \"A\" in RichTextBox?

4条回答
  •  再見小時候
    2020-11-28 15:16

    This won't work while you are typing if that is what you are looking for, but I use this to highlight substrings:

    Function Highlight(ByVal Search_Str As Object, ByVal InputTxt As String, ByVal StartTag As String, ByVal EndTag As String) As String
        Highlight = Regex.Replace(InputTxt, "(" & Regex.Escape(Search_Str) & ")", StartTag & "$1" & EndTag, RegexOptions.IgnoreCase)
    End Function
    

    and call it this way:

    Highlight("A", "Color All my A's red", [span class=highlight]', '[/span]')
    

    Where the class 'highlight' has whatever color coding/formatting you want:

    .highlight {text-decoration: none;color:black;background:red;}
    

    BTW: you need to change those square brackets to angled ones...they wouldn't come thru when I typed them...

提交回复
热议问题