Get ErrorMessage from ErrorProvider : ErrorProvider « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » ErrorProvider 
23.32.3.Get ErrorMessage from ErrorProvider
Get ErrorMessage from ErrorProvider
using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

public class TextBoxValidationRegex : Form
{
    public TextBoxValidationRegex()
    {
        this.components = new System.ComponentModel.Container();
        this.Button1 = new System.Windows.Forms.Button();
        this.errProvider = new System.Windows.Forms.ErrorProvider(this.components);
        this.Label3 = new System.Windows.Forms.Label();
        this.txtEmail = new System.Windows.Forms.TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.errProvider)).BeginInit();
        this.SuspendLayout();

        this.Button1.Location = new System.Drawing.Point(21280);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(7624);
        this.Button1.TabIndex = 12;
        this.Button1.Text = "OK";
        this.Button1.Click += new System.EventHandler(this.Button1_Click);

        this.errProvider.ContainerControl = this;
        this.errProvider.DataMember = "";

        this.Label3.Location = new System.Drawing.Point(2424);
        this.Label3.Size = new System.Drawing.Size(4016);
        this.Label3.Text = "Email:";

        this.txtEmail.Location = new System.Drawing.Point(6820);
        this.txtEmail.Size = new System.Drawing.Size(22021);
        this.txtEmail.Leave += new System.EventHandler(this.txtEmail_Leave);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(328126);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.txtEmail);
        this.Controls.Add(this.Button1);
        this.Font = new System.Drawing.Font("Tahoma"8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        ((System.ComponentModel.ISupportInitialize)(this.errProvider)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void Button1_Click(object sender, EventArgs e)
    {
        string errorText = "";

        foreach (Control ctrl in this.Controls)
        {
            if (errProvider.GetError(ctrl!= "")
            {
                errorText += "   * " + errProvider.GetError(ctrl"\n";
                
            }
            MessageBox.Show("Errors:" +errorText, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void txtEmail_Leave(object sender, EventArgs e)
    {
        Regex regex;
        regex = new Regex(@"^[\w-]+@([\w-]+\.)+[\w-]+$");

        Control ctrl = (Control)sender;
        if (regex.IsMatch(ctrl.Text|| ctrl.Text == "")
        {
            errProvider.SetError(ctrl, "");
        }
        else
        {
            errProvider.SetError(ctrl, "This is not a valid email address.");
        }
    }

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new TextBoxValidationRegex());
    }
    private System.Windows.Forms.Button Button1;
    private System.Windows.Forms.ErrorProvider errProvider;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.TextBox txtEmail;

    private System.ComponentModel.IContainer components = null;

}
23.32.ErrorProvider
23.32.1.ErrorProvider: number must be in a range
23.32.2.ErrorProvider: Text lengthErrorProvider: Text length
23.32.3.Get ErrorMessage from ErrorProviderGet ErrorMessage from ErrorProvider
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.