【问题标题】:How to search for a phrase in a RichTextBox and highlight every instance of that phrase with a color you specify如何在 RichTextBox 中搜索短语并使用您指定的颜色突出显示该短语的每个实例
【发布时间】:2012-06-09 05:06:47
【问题描述】:

如何在 RichTextBox 中搜索短语并以所需的任何颜色突出显示该短语的每个实例?

【问题讨论】:

    标签: c# winforms richtextbox


    【解决方案1】:

    根据需要替换分配给 ValToSearchFor 的字符串和分配给 RichTextBox 的 SelectionColor 属性的颜色。

    String richText = richTextBox1.Text;
    String ValToSearchFor = "duckbilledPlatypus";
    int pos = 0;
    pos = richText.IndexOf(ValToSearchFor, 0);
    while (pos != -1) {
        richTextBox1.Select(pos, ValToSearchFor.Length);
        richTextBox1.SelectionColor = Color.Red;
        pos = richText.IndexOf(ValToSearchFor, pos + 1);
    }
    

    【讨论】:

    • 为了支持大文本,您只能查看屏幕上可见的字符。这可能会提高大文本的性能。许多语法高亮引擎都这样做。
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2015-03-07
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多