RichTextBox-如何突出显示行尾?

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

如果搜索到的单词在该行的开头或结尾处,我想为其着色。如果它在行的中间,则不要五颜六色。我尝试了很多事情,但是工作不正常。

似乎行首正在运行。但是第一行的末尾只能上色。我要为所有行结尾着色。我认为我需要每一行的开始和循环索引,但是我做不到。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Dejg1Qi5qcGcifQ==” alt =“我的无效程序”>

我该如何解决?

 private void button1_Click(object sender, EventArgs e)
    {
        int wordLength = textBox1.Text.Length;
        string word = textBox1.Text;     
        for (int i = 0; i < richTextBox1.Lines.Count(); i++)
        {                
            int startIndex = richTextBox1.GetFirstCharIndexFromLine(i);
            richTextBox1.Find(word, startIndex, startIndex+wordLength, RichTextBoxFinds.None);
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectionBackColor = Color.Yellow;

            int newLineIndex = richTextBox1.Lines[i].Length;
            richTextBox1.Find(textBox1.Text, (newLineIndex - wordLength), newLineIndex, RichTextBoxFinds.None);
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.SelectionBackColor = Color.Yellow;
        }         
c# winforms richtextbox
1个回答
0
投票

尝试:

 int newLineIndex = i + 1 < richTextBox1.Lines.Length ? richTextBox1.GetFirstCharIndexFromLine(i + 1) - 1 : richTextBox1.TextLength;
© www.soinside.com 2019 - 2024. All rights reserved.