C# - 实时显示多行文本框计数

标签 c# list counter multiline

我有一个多行文本框,我可以在其中键入或粘贴项目。在文本框的底部,我有一个“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。在大多数情况下,您需要显式执行转换。

关于C# - 实时显示多行文本框计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898939/

相关文章:

c# - IIS6、ASP.NET MVC 1 和随机减速

caching - MongoDB 缓存计数器

python - 检查 Python 的 `Counter` 中的元素数量

c# - 使用 Graphics.GetHdc 时参数无效异常

c# - 类型转换如何从 double 到 byte 工作

c# - 从 IdentityServer4 openIdConnect 获取 IsPersistent 设置

python - 使用 django 实现一个计算请求的计数器

计算 C 列表中的符号变化

java - 自制迭代器bug

xml - SQL Server 2008搜索和替换xml节点值