Question

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.

Was it helpful?

Solution 5

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.

OTHER TIPS

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 ...

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top