Error Provider to validate the text in a TextBox : Validation « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ValidationScreenshots 
Error Provider to validate the text in a TextBox
Error Provider to validate the text in a TextBox


  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class ErrorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button btnValidate;
    private System.Windows.Forms.ErrorProvider errorProvider1;
    private System.Windows.Forms.TextBox txtInput;

    public ErrorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
      this.label1 = new System.Windows.Forms.Label();
      this.txtInput = new System.Windows.Forms.TextBox();
      this.btnValidate = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // errorProvider1
      // 
      this.errorProvider1.BlinkRate = 500;
      this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(88);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(37656);
      this.label1.TabIndex = 2;
      this.label1.Text = "The following text box only allows 5 characters.";
      // 
      // txtInput
      // 
      this.txtInput.Location = new System.Drawing.Point(14480);
      this.txtInput.Name = "txtInput";
      this.txtInput.Size = new System.Drawing.Size(12020);
      this.txtInput.TabIndex = 0;
      this.txtInput.Text = "";
      this.txtInput.Validating += new System.ComponentModel.CancelEventHandler(this.txtInput_Validating);
      // 
      // btnValidate
      // 
      this.btnValidate.Location = new System.Drawing.Point(1672);
      this.btnValidate.Name = "btnValidate";
      this.btnValidate.Size = new System.Drawing.Size(11232);
      this.btnValidate.TabIndex = 1;
      this.btnValidate.Text = "OK";
      // 
      // ErrorForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(400125);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label1,
                                      this.btnValidate,
                                      this.txtInput});
      this.Name = "ErrorForm";
      this.Text = "Error Trapper";
      this.ResumeLayout(false);

    }

    static void Main() 
    {
      Application.Run(new ErrorForm());
    }

    private void txtInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      if(txtInput.Text.ToString().Length > 5) {
        errorProvider1.SetErrortxtInput, "Can't be greater than 5!");
      
      else{
        errorProvider1.SetError(txtInput, "");
        }
    }
  }

           
       
Related examples in the same category
1.TextBox value validation DemoTextBox value validation Demo
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.