Get/Set First Visible Line of RichTextBox

微笑、不失礼 提交于 2021-02-18 10:45:31

问题


I have a RichTextBox with thousands of lines of text in it. I can easily SET the first visible line by using ScrollToCaret() by doing...

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

But I would like to be able to GET the first visible line too. Any suggestions?


回答1:


Here may be what you need:

//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];

For your comment saying that you want some line accordingly to the Width of the RichTextBox, you can do something like this:

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);


来源:https://stackoverflow.com/questions/18134683/get-set-first-visible-line-of-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!