I’ve got a situation (a key-based search) where I’m going to have multiple inputs of varying, but constrained, length. So I’m building a common control to use for each input, and this control has a TextBox for use when the input type is free-form text. In that case I’d like the length of the TextBox to reflect the constraint placed on the input based on the TextBox’s MaxLength property (or at least on a maximum length of some sort).
How can I do this? I realize this is technically impossible with variable width characters, but I’m interested in getting close. The fields vary in maximum length from 3 characters to around 90.
Based on what’s been said so far, I think the ideal solution in this case is a combination of John’s suggestion and that put forth by Mitchel and overslacked. So, I’m going to calculate the lengths using a standard character (say “C”), and then set the width equal to [border slack] + ([character length] * ([number of characters] + [5 – number of characters Mod 5])).
I’ll have to recalculate the character length and the field width any time the font changes.