Detect if all text in TextBox is visible
Posted on 2006-06-09
Wish to change the background color of a TextBox if the text is not all displayed.
Wish this to work with Multiline True or False, as well as for different client rectangle sizes, and different font sizes and bold/italic etc.
Purpose is to let user know that there is more text than is shown.
Tried this solution, but it is not exact.
' Control inherits from TextBox
Protected Overrides Sub OnTextChanged (ByVal e as System.EventArgs)
' If it all fits, then background color is Window, else Control
Dim clientWidth as Integer = ClientSize.Width
Dim clientHeight as Integer = ClientSize.height
Dim grfx as Graphics = CreateGraphics
Dim fudgeWidth as Double = grfx.MeasureString(" X", Font, Integer.MaxValue)
Dim actualSizeF as SizeF = grfx.MeasureString(Text, Font, clientWidth + fudgeWidth)
If actualSizef.Height <= clientHeight then
BackColor = Drawing.SystemColors.Window
BackColor = Drawing.SystemColors.Control
' This works most of the time, but would like it to work all the time.
' Does TextBox use vanilla DrawString, or does it use some proprietary method of drawing text?
' Alternative is to build my own TextBox from scratch, but that doesn't sound pleasant!