Calculate length of textBox for N number of characters
-
How can I calculate the required length of a textBox to hold N characters please?
-
How can I calculate the required length of a textBox to hold N characters please?
I dont think you can.. unless you know ahead of time what each character is going to be. A fair approximation would be 9xN since the default text is at 8.5 points. But this leave the box looking a bit "longer" than needed if it contains mostly "skinny" chars. Consider these 10 char field widths: OPOPOPOPOPO itititititi Perhaps if there is a setting that forces the character spacing to be equal - then you can certainly make a calculation.
--------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler
-
How can I calculate the required length of a textBox to hold N characters please?
Well, very often you can't depending on the font. Monospaced fonts like Courier you could, but for most you'll find a 'W' quite a bit wider than an 'I'. If you are using a monospaced font, you'd need to get a Graphics object, then call MeasureString (I think it is - or something like that). Which will tell you how wide a string is in pixels.
Regards, Rob Philpott.
-
How can I calculate the required length of a textBox to hold N characters please?
-
One way is, create a dummy label object(with AutoSize = true), assign the text to it. Use the labels width to set the TextBox's width :)
Many thanks to everyone who replied. I like the dummy label idea - much appreciated.