基于此线程How to get the line of specific text in richtextbox,我想问另一个有关richtextbox中特定文本行的问题。
[从那个话题,他想在哪一行显示“橙色”文字?但是,我想知道他是否还有另一个“橙色”,例如
从该表中,我想显示'橙色'文本的行号我预计输出将是“第1,2,3,4行”
非常感谢。
您必须使用循环并跟踪最后找到该字符串的索引。类似的方法应该起作用:
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”的多个实例。