Click here to Skip to main content
14,329,302 members

Get Viewable Text From Textbox

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
4 Mar 2013CPOL
Get the viewable text for your textbox.


This article provides code to get only the viewable text from a textbox. This is useful in case we want to display part of the text which is not viewable on other screens using controls like richtextbox or any other text displaying controls.


This code was developed because of a unique requirement while displaying reports in a project. In the reports there was a textarea for Narration having width = 285 and height=250. So when the user was entering a long text, the RDLC textarea used to clip off the textarea after a specific number of characters. The decision was made to display the remaining text on a supplement report. The challenge was to find the exact number of characters displayed on an RDLC textarea.

To overcome this situation I came up with a solution to have a textbox in runtime have all the same dimensions as that of a textarea in an RDLC report, like font, size, and has the following code to serve the purpose.

The below function accepts two parameters:

  1. Textbox which you need to get the viewable text from
  2. Number of viewable lines
Private Function GetViewableTextFromTextbox(ByVal textbox As _
           TextBox, ByVal textboxLines As Int32) As String

	Dim prevLineNumber As Integer = 1
	Dim currentLineNumber As Integer = 1
	Dim totalLines As Integer = 1

	Dim visibleText As String = String.Empty

	For charIndex = 0 To textbox.Text.Length - 1
	  visibleText = textbox.Text.Substring(0, charIndex)
	  Dim renderedTxtboxPoint As Point
	  renderedTxtboxPoint = textbox.GetPositionFromCharIndex(charIndex)
	  currentLineNumber = renderedTxtboxPoint.Y
	  If prevLineNumber <> currentLineNumber Then
	    prevLineNumber = currentLineNumber
	    totalLines += 1
	   If totalLines > textboxLines Then
	      GetViewableTextFromTextbox = visibleText
	      Exit For
	   End If
	  End If

	GetViewableTextFromTextbox = visibleText
End Function

To get the viewable text for your textbox, call it as below:

GetVisibleTextFromTextbox(Textbox1, 18)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ajit Amare
Technical Lead
United States United States
No Biography provided

Comments and Discussions

QuestionNice work Pin
Tom Clement4-Mar-13 16:11
mentorTom Clement4-Mar-13 16:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 4 Mar 2013

Tagged as


1 bookmarked