从Richtextbox中的特定文本中查找并显示多行

问题描述 投票:0回答:1

基于此线程How to get the line of specific text in richtextbox,我想问另一个有关richtextbox中特定文本行的问题。

[从那个话题,他想在哪一行显示“橙色”文字?但是,我想知道他是否还有另一个“橙色”,例如

Exmaple image

从该表中,我想显示'橙色'文本的行号我预计输出将是“第1,2,3,4行”

非常感谢。

c# visual-studio code-behind
1个回答
0
投票

您必须使用循环并跟踪最后找到该字符串的索引。类似的方法应该起作用:

private void button1_Click(object sender, EventArgs e)
{
    string orange = "orange";
    var index = 0;
    do
    {
        index = richTextBox1.Find(orange, index, RichTextBoxFinds.None);

        if (index >= 0)
        {
            textBox1.Text += richTextBox1.GetLineFromCharIndex(index).ToString() + " ";
            index++;
        }
    } while (index >= 0);
}

这将在不同的行中找到字符串“ orange”的多个实例。

© www.soinside.com 2019 - 2024. All rights reserved.