Scrollbars on TextBox component

M

Michael Crago

Hi there,

Is there a way to make the vertical scrollbar optional on the TextBox
component depending on the amount of text in the box?

As far as I can tell, you can set a TextBox's ScrollBars property to either
'None' or 'Vertical'. If the latter, the scrollbar is always present (and
grayed out), even if there is minimal text.

Is there a trick to making the ScrollBar's visibility text-dependant?

Or at least a way to determine when the text overfills the TextBox so I can
change ScrollBars property from None to Vertical manually?

Thank you...

Robert Crago
 
J

Jonathan Wells [msft]

Hi Michael,

You could use the Graphics.MeasureString method to obtain the length of a
string in pixels. Here is an example in c#:

private void TestString( TextBox textBox ){
Graphics g = this.CreateGraphics();
SizeF size = g.MeasureString( textBox.Text, textBox.Font );

int lines = (int)size.Width / (textBox.Width-5);
lines += (size.Width % textBox.Width)!=0? 1: 0;

if( (size.Height*lines)>textBox.Height ) {
textBox.ScrollBars = ScrollBars.Vertical;
} else {
textBox.ScrollBars = ScrollBars.None;
}
g.Dispose();
}

cheers jonathan

--
Jonathan Wells
Product Manager
..NET Compact Framework
Check out the .NET Compact Framework FAQ at:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top