text length in pixels

  • Thread starter Thread starter Vishal Gupta
  • Start date Start date
V

Vishal Gupta

Hi,

I am trying to resolve the following scenario:
We have a text box of fixed width that by default should display only 1 line
of text without a scrollbar indicator.
If there are multiple lines of text then we need to to indicate that by
displaying a button alongside the textbox that the user can click on to
expand the height of the text box. The height would be, say, 4 lines of
text. If there are more than 4 lines of text then we need to display a
vertical scrollbar.
To be able to achieve this, it seems, we need to be able to compare the
length of the text with that of the textbox. If we can somehow get the
length in pixels that would solve part of the problem?

Any ideas would be greatly appreciated...

Thanks, Vishal
 
Instead of trying to accomplish this by pixels (the hard way), try this:

A textbox that does not have any styling applied to it will use a font where
different characters are different widths (capital W is wider than lower
case l). So, pick a number of characters that you consider to be one full
line of text. You can programmatically check the number of characters in a
textbox and when you reach your one line limit, you can change the rows
property of the textbox to 4. The vertical scroll bar is built in, so you
don't have to worry about enabling it when needed.
 
Check out the .NET Framework Class Library for:

Graphics.MeasureString Method (String, Font)
 
Back
Top