加一个ErrorProvider
this.errorProvider1.ContainerControl = this;
this.errorProvider1.SetIconAlignment(this.textBox1, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.errorProvider1.SetIconPadding(this.textBox1, 2);
然后在textBox1_Validated中textBox_TextChanged事件里写
if (char.IsLetter(this.textBox1.Text.ToCharArray()[0]) != true)
this.errorProvider1.SetError(this.textBox1, "首字母必须为字母");
else if (this.textBox1.Text.Length < 5)
this.errorProvider1.SetError(this.textBox1, "必需至少有5个字符");
this.errorProvider1.ContainerControl = this;
this.errorProvider1.SetIconAlignment(this.textBox1, System.Windows.Forms.ErrorIconAlignment.MiddleRight);
this.errorProvider1.SetIconPadding(this.textBox1, 2);
然后在textBox1_Validated中textBox_TextChanged事件里写
if (char.IsLetter(this.textBox1.Text.ToCharArray()[0]) != true)
this.errorProvider1.SetError(this.textBox1, "首字母必须为字母");
else if (this.textBox1.Text.Length < 5)
this.errorProvider1.SetError(this.textBox1, "必需至少有5个字符");
本文介绍如何使用ErrorProvider控件验证TextBox的输入内容,确保首字符为字母且长度不少于5个字符。
1817

被折叠的 条评论
为什么被折叠?



