![](/img/trans.png)
[英]how to display the contents of file in multi-line textbox in readable format and in asp.net C#
[英]C# - Display Multi-line TextBox count in real-time
我有一个多行的TextBox,我可以输入或粘贴项目。在文本框的底部我有一个“Item Count =”标签和旁边的“0”文本框。
我希望“0”文本框中的文本能够实时跟踪文本框列表中的项目数。 这可能吗?
这就是我所拥有的,但我无法让它发挥作用:
private void textBox2_TextChanged(object sender, EventArgs e)
{
char[] delimiterChars = { ',', ':', '|', '\n' };
List<string> sortBox =
new List<string>(textBox_ListSource.Text.Split(delimiterChars));
var itemCount = sortBox.Count();
textBox_SourceCount.Text = itemCount;
}
我在最后一行的“itemCount”下得到一个红色的波浪形。 它不会编译,并且表示无法将'int'显式转换为'string'。
尝试
textBox_SourceCount.Text = itemCount.ToString();
此外,您不需要使用LINQ Count函数,因为List具有Count属性。
var itemCount = sortBox.Count(); // Calls a LINQ function which calls the Count property
var itemCount = sortBox.Count; // Calls the Count property directly
为了将来参考,C#不会自动将int
转换为string
。 在大多数情况下,您需要明确执行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.