Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Winform validate label text length

I have a label that is updating automatically based on other inputs. This label can only be 50 characters long. The following code is working when the apply button is clecked, butI want to check the length when the label is changed, so not just when clicking apply on the form. How should I do this?

        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);
            }
        }
question from:https://stackoverflow.com/questions/65844500/winform-validate-label-text-length

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I used the following linked to the TextChanged event:

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);
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...