[英]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.