如何从RichTextBox中获取显示的文本?
如何获取RichTextBox中显示文本? 我的意思是,如果 RichTextBox 滚动到末尾,我只想接收那些对我来说可见的行。
PS获取第一个显示的字符串就足够了
How to get displayed text in RichTextBox?
I mean if RichTextBox is scrolled to the end, I'd like to receive only those lines, which are visible for me.
P.S.It'll be enough to get fisrt displayed string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想使用 RichTextBox.GetCharIndexFromPosition()。要获取第一个可见字符的索引,请传递 new Point(0, 0),即 RTB 客户区的左上角。要获取最后一个可见字符的索引,请传递 new Point(rtb.ClientSize.Width, rtb.ClientSize.Height)。 RichTextBox.Text.Substring() 然后获取所有可见文本。
如有必要,您可以使用 RichTextBox.GetLineFromCharIndex() 将字符索引转换为行号。
You want to use RichTextBox.GetCharIndexFromPosition(). To get the index of the first visible character, pass new Point(0, 0), the upper left corner of the RTB client area. To get the index of the last visible character, pass new Point(rtb.ClientSize.Width, rtb.ClientSize.Height). RichTextBox.Text.Substring() then gets you all visible text.
If necessary, you can use RichTextBox.GetLineFromCharIndex() to translate the character indexes to line numbers.
查看通过 SendMessage API 函数发送消息 EM_GETFIRSTVISIBLELINE。
Look at sending the message EM_GETFIRSTVISIBLELINE via the SendMessage API function.
来自鸡蛋咖啡馆:
“
这个想法是获取滚动条可见区域下的文本。
您需要找出 RichTextBox 的高度,并使用控件的 TextHeight 属性确定文本的高度。将控件的高度除以文本的高度。
通过此,您可以确定 RichTextBox 控件中可以容纳的最大行数。
希望这能解决或至少让你接近。
“
取自
http://www.eggheadcafe。 com/community/aspnet/2/10073516/how-to-select-the-visible.aspx
From eggcafe:
"
The idea is to get the text under the scrollbar visible area.
You need to find out the height of the richtextbox and determine the text's height by using the TextHeight Property of the control. divide the height of the control by text's height.
By this, you can determine the maximum number of lines that can be accomodated in the richtextbox control.
Hope this resolves or atleast takes you near.
"
Taken from
http://www.eggheadcafe.com/community/aspnet/2/10073516/how-to-select-the-visible.aspx
不优雅,但我认为这可行。
如果您想允许用户选择文本等,这当然行不通。
Not elegant, but I think this works.
This of course does not work out if you want to allow the user to select text, etc.