How to get displayed text from RichTextBox?

巧了我就是萌 提交于 2020-01-12 08:52:47

问题


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


回答1:


You should use RichTextBox.GetCharIndexFromPosition(point).

To get the index of the first visible character, pass new Point(0, 0) (the upper left corner of the RTB client area) as the point parameter.
To get the index of the last visible character, pass new Point(rtb.ClientSize.Width, rtb.ClientSize.Height) as the point parameter.

You can then use RichTextBox.Text.Substring() to get all visible text.

If necessary, you can use RichTextBox.GetLineFromCharIndex() to convert the character indexes to line numbers.




回答2:


Look at sending the message EM_GETFIRSTVISIBLELINE via the SendMessage API function.




回答3:


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




回答4:


Not elegant, but I think this works.

//Force selection 

richTextBox.SelectAll();

//Get the selected text

dataString = richTextBox.Selection.Text;

This of course does not work out if you want to allow the user to select text, etc.



来源:https://stackoverflow.com/questions/2750532/how-to-get-displayed-text-from-richtextbox

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