10

我有一个RichTextBox包含数千行文本的内容。通过使用,我可以轻松地SET看到第一条可见线ScrollToCaret()......

this.SelectionStart = this.Find(this.Lines[lineIndex], RichTextBoxFinds.NoHighlight);
this.ScrollToCaret();

但我也希望能够GET看到第一条可见线。有什么建议么?

4

1 回答 1

16

这可能是您需要的:

//get the first visible char index
int firstVisibleChar = richTextBox1.GetCharIndexFromPosition(new Point(0,0));
//get the line index from the char index
int lineIndex = richTextBox1.GetLineFromCharIndex(firstVisibleChar);
//just get the string of the line
string firstVisibleLine = richTextBox1.Lines[lineIndex];

对于您的评论说您想要一些与 的 相应Width的行RichTextBox,您可以执行以下操作:

int firstVisibleChar = richTextBox1.GetCharIndexFromPosition(new Point(0,0));
int lastChar = richTextBox1.GetCharIndexFromPosition(new Point(richTextBox1.ClientSize.Width - 1, 1));
string firstVisibleLine = richTextBox1.Text.Substring(firstVisibleChar, lastChar - firstVisibleChar);
于 2013-08-08T19:54:38.323 回答