0

我正在尝试构建一个程序,在用户输入文本框时显示字符和单词的数量。我以为我知道自己在做什么,但遇到了这个错误:

'不能将类型'string'隐式转换为'Systems.Windows.Forms.Label'

这就是我到目前为止所拥有的。最后一行代码包含错误:

    private void userTextBox_TextChanged(object sender, EventArgs e)
    {
        string userInput = userTextBox.Text;
        char charCount;
        charCount = userInput[0];

        charCountOutput = charCount.ToString();
    }
4

5 回答 5

1
charCountOutput.Text = charCount.ToString();

假设 charCountOutput 是标签

您的代码正在尝试为 Label 对象分配字符串的值,这是类型不匹配(显然)。

于 2013-11-09T23:00:31.920 回答
1

1)需要在Label上设置属性来设置文字

charCountOutput.Text = ...

2)可以通过Length属性访问字符串的长度

charCountOutput.Text = userInput.Length.ToString();
于 2013-11-09T23:09:28.027 回答
0

这是一个较晚的添加 - 您可能已经看过这个,但这是一个非常快速的方法。假设 charCountOutput 是表单上的标签:

        private void userTextBox_TextChanged(object sender, EventArgs e)
    {
        var userInput = userTextBox.Text;
        charCountOutput.Text = userInput.Length.ToString();
    }
于 2014-12-22T15:31:59.267 回答
0

您正在分配一个文本字段,更改该字段的文本。

charCountOutput.Text = charCount.ToString();
于 2013-11-09T23:00:59.020 回答
0
     int countChar = userTextBox.Text.ToString().Length;
于 2013-11-09T23:20:10.887 回答