views:

462

answers:

6

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.

A: 

This is (almost) impossible with variable width fonts.

I guess you could try find a relatively 'wide' character, but that can still cause issues.

The easiest solution would probably be to find a satisfactory character width (and probably hardcode this per font size/family), and then simply multiply that with the max length.

leppie
Well, I'm mostly interested in at least getting close. I don't want a 30-character wide textbox for a 3 character field or vice-versa.
Sam Erwin
There is the MeasureString()-method for the Graphics-object which works for a given font. He could measure a big character to get a useful result ... But I would not do the whole thing ...
tanascius
+1  A: 

One way that you might be able to do this, but not 100% exact would be to use the "MeasureString" method from the Graphics object, to see how wide X characters is, with the font that is currently used by the textbox.

From there you can then calculate the needed "extra" to cover the borders etc. Not an exact science, but it would work.

Other Thoughts

Measuring the whole string might be a bit inefficient, you could measure 1 X character (which is typically larger than others) then multiply it by all

Mitchel Sellers
Your "Other thoughts" are good, because I usually won't actually have a string - just a length.
Sam Erwin
+4  A: 

I would try to avoid this dynamic approach.
Do you really think that it adds value for the user? In my opinion it looks strange, when a common control has a different look all the time. Do you think the user can differ between a textbox that allows 32 characters and one that allows 29 characters? How about extrem values? When e.g. only three characters are allowed - or even 1024.
I'd make the TextBox the same length, always - that is better for your control and easier to implement. When the user needs a hint about the max lenght of the text add a label that says "29 characters left" and that updates during the input ...

tanascius
I would normally try to, as well, but the dynamic nature of this makes me want to present a reasonable measure of accuracy in how large the field is.
Sam Erwin
+1  A: 

It would be simpler, and potentially more visually pleasing, to "chunk" the textbox sizes. Pick a width that works for fields of 1-5 characters, then a width for 6-10, then 11-20,20-30,30-50, etc. This way, you can avoid the weird visual alignment issues that would happen if you tried to vertically stack fields of 29, 32, and 27 characters.

John Fisher
This is a good idea for being consistent at varying sizes.
Sam Erwin
A: 

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.

Sam Erwin
This sort of works, but doesn't accomplish quite as tight of a text box as I'd hoped.
Sam Erwin
A: 

que sarasa que tiran yankis de mierda