private void textBox1_Validated(object sender,EventArgs e) { errorProvider1.SetError(this.textBox1,""); } public bool ValidEmailAddress(string emailAddress,out string errorMessage) { //首先判断是否为空,然后判断是否有@,.符号 if(emailAddress.Length==0) { errorMessage="e-mail address is required."; return false; } //是否包含@ if(emailAddress.IndexOf("@")>-1) { //从@往后面开始搜索,找到.的位置,如果位置大于@的位置说明格式正确 if(emailAddress.IndexOf(".",emailAddress.IndexOf("@"))>emailAddress.IndexOf("@")) { errorMessage=""; return true; } } errorMessage="e-mail address must be valid e-mail address format.\n"+"For example 'someone@example.com'"; return false; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.errorProvider1 = new System.Windows.Forms.ErrorProvider(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(72, 88); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // errorProvider1 // this.errorProvider1.ContainerControl = this; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); |