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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.