如果存在文本,请在Richtextbox中突出显示整行 [英] Highlight entire line in Richtextbox if a text is present

查看:75
本文介绍了如果存在文本,请在Richtextbox中突出显示整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图突出显示RichTextBox中的特定行,如图所示.

I'm trying to highlight a particular line in my RichTextBox as shown in figure.

int ptrsize = 10;
int* linenum; 
for (int i = 0; i < ptrsize; i++)
{           
    int value = (linenum[i]) * 10;
    string searchText = value.ToString();
    int indexToText = richTextBox.Find(searchText);
    int endIndex = searchText.Length;
    richTextBox.Select(indexToText, endIndex);
    richTextBox.SelectionColor = Color.Blue;
}

如果有文字(例如2010年),我要突出显示整行.

If a text is present (i.e. 2010), i want to highlight the entire line.

2010 19.5 7.37 105 0.67 0.26 0.69

2010 19.5 7.37 105 0.67 0.26 0.69

推荐答案

如果其中包含给定的文本,则会突出显示给定的行:

This will highlight a given line if it contains a given text:

void highlightLineContaining(RichTextBox rtb, int line, string search, Color color)
{
    int c0 = rtb.GetFirstCharIndexFromLine(line);
    int c1 = rtb.GetFirstCharIndexFromLine(line+1);
    if (c1 < 0) c1 = rtb.Text.Length;
    rtb.SelectionStart = c0;
    rtb.SelectionLength = c1 - c0;
    if (rtb.SelectedText.Contains(search))
        rtb.SelectionColor = color;
    rtb.SelectionLength = 0;
}

您可能要存储和恢复原始的选择.

You may want to store and restore the original Selection.

有时更改 SelectionBackColor 看起来更好.试试看!

Sometimes changing the SelectionBackColor looks better. Give it a try!

您可以在整个RTB上调用它:

You could call it on the whole RTB :

for (int i = 0; i < richTextBox.Lines.Count(); i++)
      highlightLineContaining(richTextBox, i, searchText, Color.Red);

这篇关于如果存在文本,请在Richtextbox中突出显示整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆