简体   繁体   English

显示当前字符/字符限制的WinForm文本控件

[英]WinForm Text Control that displays current character/character limit

I need a way to display, like you see in some web apps, the current characters/character limit for a Text Control ( ie 3/500). 我需要一种显示方式,就像您在某些Web应用程序中看到的那样,显示Text Control当前字符/字符数限制 3/500)。 I usually see this as a label residing directly above or below the Text Control . 我通常将其视为直接位于Text Control上方或下方的label

How is this 'normally' accomplished? “正常”如何完成? Should I override my Text Control somehow? 我应该以某种方式覆盖我的Text Control吗? Do I just manually add labels by every Text Control and ' bind ' them to properties of the Text Control ? 我是否只是通过每个Text Control手动添加标签并将它们“ bind ”到Text Control属性? Do I need to create a composite control that has the T ext Control & Label Controls together to accomplish what I need? 我是否需要创建一个同时包含“ ext Control和“ Label Controls的复合Label Controls来完成我所需要的?

Any direction or help will be greatly appreciated. 任何方向或帮助将不胜感激。

Attach a handler to TextBox.TextChanged and refresh the label based on TextBox.Text.Length and the maximum size you want to allow. 将处理程序附加到TextBox.TextChanged并根据TextBox.Text.Length和要允许的最大大小刷新标签。 You can also enforce the limit in the same handler by calling TextBox.Text = TextBox.Text.Substring( 0, maxChars ); 您还可以通过调用TextBox.Text = TextBox.Text.Substring( 0, maxChars );在同一处理程序中实施限制TextBox.Text = TextBox.Text.Substring( 0, maxChars ); .

int maxChars = 100;

Textbox onKeyUp:
remainingChars.Text = Convert.ToString(maxChars - textbox.Text.Length);

Stick it in a function: 将其粘贴在一个函数中:

getRemainingChars(TextBox tb, Label lbl, int max)
{
lbl.Text = Convert.ToString(max - tb.Text.Length) + "/" + Convert.ToString(max);
}

Hopefully thats of some help 希望这能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM