Tutorial :How to get displayed text from RichTextBox?


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


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.


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


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.

//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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »