添加:
自定义验证代码:
public partial class SuperTextBox : TextBox { public SuperTextBox() { InitializeComponent(); } public SuperTextBox(IContainer container) { container.Add(this); InitializeComponent(); } public int BeginCheckEmpty() { if (this.Text.Trim() == "") { this.errorProvider1.SetError(this, "必填项不能为空!"); return 0; } else { this.errorProvider1.SetError(this, string.Empty); return 1; } } public int BeginCheckData(string regularExpress, string errorMsg) { if (BeginCheckEmpty() == 0) return 0; //正则表达式验证,忽略大小写 Regex objRegex = new Regex(regularExpress, RegexOptions.IgnoreCase); if (!objRegex.IsMatch(this.Text.Trim())) { this.errorProvider1.SetError(this, errorMsg); return 0; } else { this.errorProvider1.SetError(this, string.Empty);//清楚小圆点的错误提示 return 1; } } }
调用:
private void btnAdd_Click(object sender, EventArgs e) { int a = this.txtName.BeginCheckEmpty(); int b = this.txtAge.BeginCheckEmpty(); int c = this.txtAge.BeginCheckData(@"^[1-9]\d*$", "年龄必须是正整数"); int result = a * b *c; if (result != 0) { MessageBox.Show("提交成功!", "提示信息"); } }
添加选项卡:把bll添加引用,别的项目就可以调用