TextBox value validation Demo : 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 
TextBox value validation Demo
TextBox value validation Demo

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

  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label lblName;
    private System.Windows.Forms.Label lblAddress;
    private System.Windows.Forms.Label lblOccupation;
    private System.Windows.Forms.Label lblAge;
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Button btnHelp;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.TextBox txtAddress;
    private System.Windows.Forms.TextBox txtOccupation;
    private System.Windows.Forms.TextBox txtAge;

    public Form1()
    {
      InitializeComponent();
      this.btnOK.Enabled = false;

      this.txtAddress.Tag = false;
      this.txtAge.Tag = false;
      this.txtName.Tag = false;
      this.txtOccupation.Tag = false;

      this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
      this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
      this.txtAge.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtAddress.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtOccupation.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
    }
    private void InitializeComponent()
    {
      this.lblName = new System.Windows.Forms.Label();
      this.txtName = new System.Windows.Forms.TextBox();
      this.lblAge = new System.Windows.Forms.Label();
      this.btnOK = new System.Windows.Forms.Button();
      this.txtAge = new System.Windows.Forms.TextBox();
      this.txtAddress = new System.Windows.Forms.TextBox();
      this.lblAddress = new System.Windows.Forms.Label();
      this.lblOccupation = new System.Windows.Forms.Label();
      this.txtOccupation = new System.Windows.Forms.TextBox();
      this.btnHelp = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // lblName
      // 
      this.lblName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblName.Location = new System.Drawing.Point(816);
      this.lblName.Name = "lblName";
      this.lblName.Size = new System.Drawing.Size(9223);
      this.lblName.TabIndex = 0;
      this.lblName.Text = "Name";
      // 
      // txtName
      // 
      this.txtName.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtName.Location = new System.Drawing.Point(11216);
      this.txtName.Name = "txtName";
      this.txtName.Size = new System.Drawing.Size(17620);
      this.txtName.TabIndex = 5;
      this.txtName.Text = "";
      // 
      // lblAge
      // 
      this.lblAge.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblAge.Location = new System.Drawing.Point(8144);
      this.lblAge.Name = "lblAge";
      this.lblAge.Size = new System.Drawing.Size(9223);
      this.lblAge.TabIndex = 3;
      this.lblAge.Text = "Age";
      // 
      // btnOK
      // 
      this.btnOK.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.btnOK.Location = new System.Drawing.Point(29616);
      this.btnOK.Name = "btnOK";
      this.btnOK.TabIndex = 10;
      this.btnOK.Text = "OK";
      this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
      // 
      // txtAge
      // 
      this.txtAge.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtAge.Location = new System.Drawing.Point(112136);
      this.txtAge.Name = "txtAge";
      this.txtAge.Size = new System.Drawing.Size(17620);
      this.txtAge.TabIndex = 8;
      this.txtAge.Text = "";
      // 
      // txtAddress
      // 
      this.txtAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtAddress.Location = new System.Drawing.Point(11240);
      this.txtAddress.Multiline = true;
      this.txtAddress.Name = "txtAddress";
      this.txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Both;
      this.txtAddress.Size = new System.Drawing.Size(17672);
      this.txtAddress.TabIndex = 6;
      this.txtAddress.Text = "";
      // 
      // lblAddress
      // 
      this.lblAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblAddress.Location = new System.Drawing.Point(840);
      this.lblAddress.Name = "lblAddress";
      this.lblAddress.Size = new System.Drawing.Size(9223);
      this.lblAddress.TabIndex = 1;
      this.lblAddress.Text = "Address";
      // 
      // lblOccupation
      // 
      this.lblOccupation.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblOccupation.Location = new System.Drawing.Point(8120);
      this.lblOccupation.Name = "lblOccupation";
      this.lblOccupation.Size = new System.Drawing.Size(9223);
      this.lblOccupation.TabIndex = 2;
      this.lblOccupation.Text = "Occupation";
      // 
      // txtOccupation
      // 
      this.txtOccupation.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
        | System.Windows.Forms.AnchorStyles.Left
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtOccupation.Location = new System.Drawing.Point(112112);
      this.txtOccupation.Name = "txtOccupation";
      this.txtOccupation.Size = new System.Drawing.Size(17620);
      this.txtOccupation.TabIndex = 7;
      this.txtOccupation.Text = "";
      // 
      // btnHelp
      // 
      this.btnHelp.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.btnHelp.CausesValidation = false;
      this.btnHelp.Location = new System.Drawing.Point(29648);
      this.btnHelp.Name = "btnHelp";
      this.btnHelp.TabIndex = 11;
      this.btnHelp.Text = "Help";
      this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(376253);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnHelp,
                                                                       this.btnOK,
                                                                       this.txtAge,
                                                                       this.txtOccupation,
                                     this.txtAddress,
                                     this.txtName,
                                     this.lblAge,
                                     this.lblOccupation,
                                     this.lblAddress,
                                     this.lblName});
      this.MinimumSize = new System.Drawing.Size(384280);
      this.Name = "Form1";
      this.Text = "TestBoxTest";
      this.ResumeLayout(false);

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

    private void btnOK_Click(object sender, System.EventArgs e)
    {
      string output;
      output = "Name: " this.txtName.Text + "\r\n";
      output += "Address: " this.txtAddress.Text + "\r\n";
      output += "Occupation: " this.txtOccupation.Text + "\r\n";
      output += "Age: " this.txtAge.Text;

      Console.WriteLine(output);
    }

    private void btnHelp_Click(object sender, System.EventArgs e)
    {
      string output;

      output = "Name = Your name" "\r\n";
      output += "Address = Your address" "\r\n";
      output += "Occupation = Only allowed value is 'Employee' or empty" "\r\n";
      output += "Age = Your age";

      Console.WriteLine(output);
    }

    private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      TextBox tb;

      tb = (TextBox)sender;

      if (tb.Text.Length == 0)
      {
        tb.BackColor = Color.Red;
        tb.Tag = false;
      }
      else
      {
        tb.BackColor = System.Drawing.SystemColors.Window;
        tb.Tag = true;
      }
      ValidateAll();
    }

    private void ValidateAll()
    {
      this.btnOK.Enabled = ((bool)(this.txtAddress.Tag&&
        (bool)(this.txtAge.Tag&&
        (bool)(this.txtName.Tag&&
        (bool)(this.txtOccupation.Tag));
    }

    private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      TextBox tb = (TextBox)sender;

      if (tb.Text.CompareTo("Employee"== || tb.Text.Length == 0)
      {
        tb.Tag = true;
        tb.BackColor = System.Drawing.SystemColors.Window;
      }
      else
      {
        tb.Tag = false;
        tb.BackColor = Color.Red;
      }

      ValidateAll();
    }

    private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
      if ((e.KeyChar < 48 || e.KeyChar > 57&& e.KeyChar != 8)
        e.Handled = true// Remove the character
    }

    private void txtBox_TextChanged(object sender, System.EventArgs e)
    {
      TextBox tb = (TextBox)sender;

      if (tb.Text.Length == && tb != txtOccupation)
      {
        tb.Tag = false;
        tb.BackColor = Color.Red;
      }
      else if (tb == txtOccupation && (tb.Text.Length != && tb.Text.CompareTo("Employee"!= 0))
      {
        tb.Tag = false;
      }
      else
      {
        tb.Tag = true;
        tb.BackColor = SystemColors.Window;
      }

      ValidateAll();
    }
  }


           
       
Related examples in the same category
1.Error Provider to validate the text in a TextBoxError Provider to validate the text in a TextBox
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.