获取/设置RichTextBox的第一个可见行

Mic*_*kus 9 .net c# scroll richtextbox winforms

我有一个RichTextBox包含数千行文字的文章.通过做... 我可以很容易地SET通过使用第一条可见线ScrollToCaret()

this.SelectionStart = this.Find(this.Lines[lineIndex], RichTextBoxFinds.NoHighlight);
this.ScrollToCaret();
Run Code Online (Sandbox Code Playgroud)

但我希望能够GET成为第一条可见线.有什么建议?

Kin*_*ing 14

这可能是你需要的:

//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];
Run Code Online (Sandbox Code Playgroud)

对于您的评论说,你需要一些行据此向WidthRichTextBox,你可以这样做:

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);
Run Code Online (Sandbox Code Playgroud)