C# Winform验证标签文本长度

C# Winform验证标签文本长度,c#,winforms,validation,C#,Winforms,Validation,我有一个基于其他输入自动更新的标签。此标签的长度只能为50个字符。下面的代码在“应用”按钮被勾选时起作用,但我想检查标签更改时的长度,而不仅仅是在表单上单击“应用”时。我该怎么做 private void labelDescription_Validating(object sender, CancelEventArgs e) { if (labelDescription.Text.Count() > 50)

我有一个基于其他输入自动更新的标签。此标签的长度只能为50个字符。下面的代码在“应用”按钮被勾选时起作用,但我想检查标签更改时的长度,而不仅仅是在表单上单击“应用”时。我该怎么做

        private void labelDescription_Validating(object sender, CancelEventArgs e)
        {
            if (labelDescription.Text.Count() > 50)
            {
                //e.Cancel = true;
                errorProvider.SetError(labelDescription, "Please review your description and shorten to a maximum of 50 characters.");
            }
            else
            {
                //e.Cancel = false;
                errorProvider.SetError(labelDescription, null);
            }
        }

我使用了以下链接到TextChanged事件的链接:

private void labelDescription_TextChanged(object sender, EventArgs e)
        {
            int noCharacters = labelDescription.Text.Count();

            if (noCharacters > 50)
            {
                errorProvider.SetError(labelDescription, "Please review your description and shorten to a maximum of 50 characters.");
            }
            else
            {
                errorProvider.SetError(labelDescription, null);
            }
        }

标签通常用来蘸东西。您应该对用户用于输入数据的任何元素进行输入验证。也就是说,我建议-标签有一个-事件。我理解,但我的标签是显示多个用户输入的串联。这些输入的组合长度不能超过50个字符。不必使用验证/验证事件,您可以在TextChanged事件中设置相同的条件。@Jimi,我就是这么做的。我想我也需要CancelEventArgs,但我没有它就可以工作。
labelDescription.Text.Length