根据 MaxLength 调整 WinForms 文本框控件的大小

发布于 2024-07-29 21:38:25 字数 260 浏览 3 评论 0原文

我遇到了一种情况(基于键的搜索),我将有多个长度不同但受限的输入。 因此,我正在构建一个用于每个输入的通用控件,并且该控件有一个 TextBox,供输入类型为自由格式文本时使用。 在这种情况下,我希望 TextBox 的长度能够反映基于 TextBox 的 MaxLength 属性(或至少某种最大长度)对输入施加的约束。

我怎样才能做到这一点? 我意识到这对于可变宽度字符在技术上是不可能的,但我有兴趣接近。 这些字段的最大长度从 3 个字符到大约 90 个字符不等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黯淡〆 2024-08-05 21:38:28

将文本框大小“分块”会更简单,并且可能更美观。 选择适用于 1-5 个字符字段的宽度,然后选择适用于 6-10 个字符的宽度,然后选择 11-20,20-30,30-50 等。这样,您可以避免可能发生的奇怪的视觉对齐问题如果您尝试垂直堆叠 29、32 和 27 个字符的字段。

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.

江城子 2024-08-05 21:38:28

根据到目前为止所说的内容,我认为这种情况下理想的解决方案是将约翰的建议与米切尔和 overslacked 提出的建议结合起来。 因此,我将使用标准字符(例如“C”)计算长度,然后将宽度设置为等于 [border slack] + ([字符长度] * ([字符数] + [5 - 数字字符数 Mod 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.

不语却知心 2024-08-05 21:38:28

对于可变宽度字体,这(几乎)是不可能的。

我想你可以尝试找到一个相对“宽”的字符,但这仍然会导致问题。

最简单的解决方案可能是找到一个令人满意的字符宽度(并且可能根据字体大小/系列对其进行硬编码),然后简单地将其乘以最大长度。

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.

滥情稳全场 2024-08-05 21:38:27

您可以执行此操作的一种方法(但不是 100% 准确)是使用 Graphics 对象中的“MeasureString”方法,以查看 X 字符的宽度以及文本框当前使用的字体。

然后,您可以从那里计算覆盖边界等所需的“额外”。这不是一门精确的科学,但它会起作用。

其他想法

测量整个字符串可能效率有点低,您可以测量 1 X 个字符(通常比其他字符大),然后将其乘以所有字符

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

聆听风音 2024-08-05 21:38:27

我会尽量避免这种动态方法。
您真的认为它为用户增加了价值吗? 在我看来,当一个通用控件始终具有不同的外观时,这看起来很奇怪。 您认为用户可以区分允许 32 个字符的文本框和允许 29 个字符的文本框吗? 极值怎么样? 例如,仅允许使用三个字符 - 甚至 1024 个字符。
我总是将文本框设置为相同的长度 - 这对您的控制更好并且更容易实现。 当用户需要有关文本最大长度的提示时,添加一个标签,上面写着“剩下 29 个字符”,并在输入过程中更新......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文