突出显示RichTextBox1中的一行,行号=一个变量 [英] Highlighting a line in a RichTextBox1, line number = a variable
问题描述
我有一个变量,可以说它= 5,然后我想在我的RichTextBox1中将行号5突出显示为蓝色。
I have a variable, lets say it = 5, and then I would like the line number 5 to be highlighted "blue" in my RichTextBox1. is that possible at all?
还是应该使用诸如ListBox,DataGridView等之类的东西?
Or should I use something like a ListBox, DataGridView etc.
推荐答案
这将突出显示<$ c $中给定行中的文本c> RichTextBox 如果 WordWrap
已关闭:
This will highlight the text in a given line in a RichTextBox
if WordWrap
is off:
void highLightALine(RichTextBox rtb, int line, Color hiLight)
{
int i1 = rtb.GetFirstCharIndexFromLine(line);
int i2 = rtb.GetFirstCharIndexFromLine(line + 1);
if (i2 < 0) i2 = rtb.Text.Length;
rtb.SelectionStart = i1;
rtb.SelectionLength = i2 - i1;
rtb.SelectionBackColor = hiLight;
}
请注意,如果 WordWrap
是的,它仍将突出显示该行,但仅在可见范围内。
Note that if WordWrap
is true it will still highlight the line but only as far as it is visible. Its continuation on the next line will not be changed.
还要注意,只有 Text
可以突出显示。 尾随的空白无法突出显示。 此处是尝试所有者绘制RTB子类的示例。
Also note that only Text
can be highlighted. Trailing empty space can't be highlighted afaik. Here is an example of trying to owner-draw an RTB subclass..
这篇关于突出显示RichTextBox1中的一行,行号=一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!