Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Get Viewable Text From Textbox

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Mar 2013CPOL1 min read 8.7K   1   1
Get the viewable text for your textbox.

Introduction

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.

Background

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
VB
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
	Next

	GetViewableTextFromTextbox = visibleText
End Function

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

VB
GetVisibleTextFromTextbox(Textbox1, 18)

License

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


Written By
Technical Lead
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice work Pin
Tom Clement4-Mar-13 16:11
professionalTom 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.