使用 C# 确定输入到文本框中的字符数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9956382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:23:04  来源:igfitidea点击:

Determine number of characters entered into textbox with C#

c#stringtexttextboxint

提问by John_Dong

Hi I was wanting to display the number of characters entered into a textbox and I want it to update as I type how can I go about this?

嗨,我想显示输入到文本框中的字符数,我希望它在我输入时更新,我该怎么做?

Here is what I have:

这是我所拥有的:

int kk = textBox1.MaxLength;
int jj = //This is what I need to know.
string lol = jj.ToString() + "/" + kk.ToString();
label2.Text = lol;

采纳答案by Jon Skeet

How about

怎么样

int jj = textBox1.Text.Length;

Or am I missing something?

或者我错过了什么?

回答by Stuart Golodetz

The text of the text box will be a string, so it has a Lengthproperty, i.e.:

文本框的文本将是一个字符串,因此它有一个Length属性,即:

textBox1.Text.Length

回答by Mamdouh Emam

you can use the OnTextChanged event of the Textbox on the client side in Javascript and compute the increment from.

您可以在 Javascript 中使用客户端文本框的 OnTextChanged 事件并计算增量。

回答by Kishore Kumar

TextBoxobject.Text.Lengthwill give you the length of textbox value.

TextBoxobject.Text.Length会给你文本框值的长度。