获取/设置RichTextBox的第一条可见线 [英] Get/Set First Visible Line of RichTextBox
本文介绍了获取/设置RichTextBox的第一条可见线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 RichTextBox
,其中包含数千行文本.通过执行以下操作,我可以轻松地使用 ScrollToCaret()
SET
来显示第一个可见行.
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();
但是我也希望能够 GET
第一行.有什么建议吗?
But I would like to be able to GET
the first visible line too. Any suggestions?
推荐答案
以下可能是您需要的:
//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];
对于说您想要一些与 RichTextBox
的 Width
相对应的行的评论,您可以执行以下操作:
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);
这篇关于获取/设置RichTextBox的第一条可见线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文