繁体   English   中英

突出显示RichTextBox C#中的整行

[英]Highlight Entire Line in RichTextBox C#

我的C#winforms应用程序中有一个richtextbox,无法找到突出显示整行的方法(文本部分+任何空白)。 我只能使用以下代码在行上突出显示文本:

firstCharIndex = richTextBox.Text.IndexOf(header);
headerLine = richTextBox.GetLineFromCharIndex(firstCharIndex);
headerLineText = richTextBox.Lines[headerLine];

int lastCharIndex = richTextBox.GetFirstCharIndexFromLine(headerLine + 1);

//richTextBox.Select(firstCharIndex, headerLineText.Length);
richTextBox.Select(firstCharIndex, lastCharIndex - firstCharIndex);

if (richTextBox.SelectionBackColor != System.Drawing.Color.DarkGray)
{
    richTextBox.SelectionBackColor = System.Drawing.Color.DarkGray;
}

使用“选择”的第二个重载进行较少的计算。

查看评论。

        //Rich text box
        richTextBox1.Text = "test test1 test test                              ";

        //Get the first index in the line from where you want to select.
        int firstCharIndex = richTextBox1.Text.IndexOf("test1");

        //Get the line based on character index.
        int line = richTextBox1.GetLineFromCharIndex(firstCharIndex);

        //Get the line it self.
        string headerLineText = richTextBox1.Lines[line];

        //Use second ovrload of select method and pass length of the line as second parameter
        //with the below approach, you can start highlighting from any where in the line.
        richTextBox1.Select(firstCharIndex, headerLineText.Length- firstCharIndex);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM