Validate text box value : Validation « 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 » Validation 
23.87.1.Validate text box value
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


   public class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         this.buttonOK.Enabled = false;
         this.textBoxAddress.Tag = false;
         this.textBoxAge.Tag = false;
         this.textBoxName.Tag = false;

         this.textBoxName.Validating += new CancelEventHandler(textBoxEmpty_Validating);
         this.textBoxAddress.Validating += new CancelEventHandler(textBoxEmpty_Validating);
         this.textBoxAge.Validating += new CancelEventHandler(textBoxEmpty_Validating);
      }

      void textBoxEmpty_Validating(object sender, CancelEventArgs e)
      {
         TextBox 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;
         }
         ValidateOK();
      }

      private void buttonOK_Click(object sender, EventArgs e)
      {
         string output;
         output = this.textBoxName.Text + "\r\n";
         this.textBoxOutput.Text = output;
      }
      private void buttonHelp_Click(object sender, EventArgs e)
      {
         this.textBoxOutput.Text = "No";
      }

      private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
      {
         if ((e.KeyChar < 48 || e.KeyChar > 57&& e.KeyChar != 8)
            e.Handled = true
      }

      private void textBox_TextChanged(object sender, EventArgs e)
      {
         TextBox tb = (TextBox)sender;
         if (tb.Text.Length == 0)
         {
            tb.Tag = false;
            tb.BackColor = Color.Red;
         }
         else
         {
            tb.Tag = true;
            tb.BackColor = SystemColors.Window;
         }
         ValidateOK();
      }

      private void ValidateOK()
      {
         this.buttonOK.Enabled = ((bool)(this.textBoxAddress.Tag&&
                                 (bool)(this.textBoxAge.Tag&&
                                 (bool)(this.textBoxName.Tag));
      }

      private void InitializeComponent()
      {
         this.labelName = new System.Windows.Forms.Label();
         this.textBoxName = new System.Windows.Forms.TextBox();
         this.buttonOK = new System.Windows.Forms.Button();
         this.textBoxAddress = new System.Windows.Forms.TextBox();
         this.labelAddress = new System.Windows.Forms.Label();
         this.buttonHelp = new System.Windows.Forms.Button();
         this.labelAge = new System.Windows.Forms.Label();
         this.textBoxAge = new System.Windows.Forms.TextBox();
         this.labelOutput = new System.Windows.Forms.Label();
         this.textBoxOutput = new System.Windows.Forms.TextBox();
         this.checkBoxProgrammer = new System.Windows.Forms.CheckBox();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.radioButtonMale = new System.Windows.Forms.RadioButton();
         this.radioButtonFemale = new System.Windows.Forms.RadioButton();
         this.groupBox1.SuspendLayout();
         this.SuspendLayout();
         // 
         // labelName
         // 
         this.labelName.AutoSize = true;
         this.labelName.Location = new System.Drawing.Point(129);
         this.labelName.Name = "labelName";
         this.labelName.Size = new System.Drawing.Size(3513);
         this.labelName.TabIndex = 0;
         this.labelName.Text = "Name";
         // 
         // textBoxName
         // 
         this.textBoxName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                     | System.Windows.Forms.AnchorStyles.Right)));
         this.textBoxName.Location = new System.Drawing.Point(986);
         this.textBoxName.Name = "textBoxName";
         this.textBoxName.Size = new System.Drawing.Size(27720);
         this.textBoxName.TabIndex = 1;
         this.textBoxName.TextChanged += new System.EventHandler(this.textBox_TextChanged);
         // 
         // buttonOK
         // 
         this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
         this.buttonOK.Location = new System.Drawing.Point(3814);
         this.buttonOK.Name = "buttonOK";
         this.buttonOK.Size = new System.Drawing.Size(7523);
         this.buttonOK.TabIndex = 2;
         this.buttonOK.Text = "OK";
         this.buttonOK.UseVisualStyleBackColor = true;
         this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
         // 
         // textBoxAddress
         // 
         this.textBoxAddress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                     | System.Windows.Forms.AnchorStyles.Right)));
         this.textBoxAddress.Location = new System.Drawing.Point(9832);
         this.textBoxAddress.Multiline = true;
         this.textBoxAddress.Name = "textBoxAddress";
         this.textBoxAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
         this.textBoxAddress.Size = new System.Drawing.Size(277100);
         this.textBoxAddress.TabIndex = 3;
         this.textBoxAddress.TextChanged += new System.EventHandler(this.textBox_TextChanged);
         // 
         // labelAddress
         // 
         this.labelAddress.AutoSize = true;
         this.labelAddress.Location = new System.Drawing.Point(1235);
         this.labelAddress.Name = "labelAddress";
         this.labelAddress.Size = new System.Drawing.Size(4513);
         this.labelAddress.TabIndex = 4;
         this.labelAddress.Text = "Address";
         // 
         // buttonHelp
         // 
         this.buttonHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
         this.buttonHelp.CausesValidation = false;
         this.buttonHelp.Location = new System.Drawing.Point(38130);
         this.buttonHelp.Name = "buttonHelp";
         this.buttonHelp.Size = new System.Drawing.Size(7523);
         this.buttonHelp.TabIndex = 5;
         this.buttonHelp.Text = "Help";
         this.buttonHelp.UseVisualStyleBackColor = true;
         this.buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);
         // 
         // labelAge
         // 
         this.labelAge.AutoSize = true;
         this.labelAge.Location = new System.Drawing.Point(12255);
         this.labelAge.Name = "labelAge";
         this.labelAge.Size = new System.Drawing.Size(2613);
         this.labelAge.TabIndex = 8;
         this.labelAge.Text = "Age";
         // 
         // textBoxAge
         // 
         this.textBoxAge.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                     | System.Windows.Forms.AnchorStyles.Right)));
         this.textBoxAge.Location = new System.Drawing.Point(98252);
         this.textBoxAge.MaxLength = 3;
         this.textBoxAge.Name = "textBoxAge";
         this.textBoxAge.Size = new System.Drawing.Size(27720);
         this.textBoxAge.TabIndex = 9;
         this.textBoxAge.TextChanged += new System.EventHandler(this.textBox_TextChanged);
         this.textBoxAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxAge_KeyPress);
         // 
         // labelOutput
         // 
         this.labelOutput.AutoSize = true;
         this.labelOutput.Location = new System.Drawing.Point(12271);
         this.labelOutput.Name = "labelOutput";
         this.labelOutput.Size = new System.Drawing.Size(3913);
         this.labelOutput.TabIndex = 10;
         this.labelOutput.Text = "Output";
         // 
         // textBoxOutput
         // 
         this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                     | System.Windows.Forms.AnchorStyles.Left)
                     | System.Windows.Forms.AnchorStyles.Right)));
         this.textBoxOutput.Location = new System.Drawing.Point(15287);
         this.textBoxOutput.Multiline = true;
         this.textBoxOutput.Name = "textBoxOutput";
         this.textBoxOutput.ReadOnly = true;
         this.textBoxOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
         this.textBoxOutput.Size = new System.Drawing.Size(360168);
         this.textBoxOutput.TabIndex = 11;
         // 
         // checkBoxProgrammer
         // 
         this.checkBoxProgrammer.AutoSize = true;
         this.checkBoxProgrammer.Checked = true;
         this.checkBoxProgrammer.CheckState = System.Windows.Forms.CheckState.Checked;
         this.checkBoxProgrammer.Location = new System.Drawing.Point(15138);
         this.checkBoxProgrammer.Name = "checkBoxProgrammer";
         this.checkBoxProgrammer.Size = new System.Drawing.Size(8817);
         this.checkBoxProgrammer.TabIndex = 12;
         this.checkBoxProgrammer.Text = "Programmer?";
         this.checkBoxProgrammer.UseVisualStyleBackColor = true;
         // 
         // groupBox1
         // 
         this.groupBox1.Controls.Add(this.radioButtonMale);
         this.groupBox1.Controls.Add(this.radioButtonFemale);
         this.groupBox1.Location = new System.Drawing.Point(15161);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.Size = new System.Drawing.Size(36085);
         this.groupBox1.TabIndex = 13;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Sex";
         // 
         // radioButtonMale
         // 
         this.radioButtonMale.AutoSize = true;
         this.radioButtonMale.Checked = true;
         this.radioButtonMale.Location = new System.Drawing.Point(18733);
         this.radioButtonMale.Name = "radioButtonMale";
         this.radioButtonMale.Size = new System.Drawing.Size(4817);
         this.radioButtonMale.TabIndex = 1;
         this.radioButtonMale.TabStop = true;
         this.radioButtonMale.Text = "Male";
         this.radioButtonMale.UseVisualStyleBackColor = true;
         // 
         // radioButtonFemale
         // 
         this.radioButtonFemale.AutoSize = true;
         this.radioButtonFemale.Location = new System.Drawing.Point(5833);
         this.radioButtonFemale.Name = "radioButtonFemale";
         this.radioButtonFemale.Size = new System.Drawing.Size(5917);
         this.radioButtonFemale.TabIndex = 0;
         this.radioButtonFemale.Text = "Female";
         this.radioButtonFemale.UseVisualStyleBackColor = true;
         // 
         // Form1
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(468466);
         this.Controls.Add(this.groupBox1);
         this.Controls.Add(this.checkBoxProgrammer);
         this.Controls.Add(this.textBoxOutput);
         this.Controls.Add(this.labelOutput);
         this.Controls.Add(this.textBoxAge);
         this.Controls.Add(this.labelAge);
         this.Controls.Add(this.buttonHelp);
         this.Controls.Add(this.labelAddress);
         this.Controls.Add(this.textBoxAddress);
         this.Controls.Add(this.buttonOK);
         this.Controls.Add(this.textBoxName);
         this.Controls.Add(this.labelName);
         this.MinimumSize = new System.Drawing.Size(484370);
         this.Name = "Form1";
         this.Text = "Form1";
         this.groupBox1.ResumeLayout(false);
         this.groupBox1.PerformLayout();
         this.ResumeLayout(false);
         this.PerformLayout();
      }
      private System.Windows.Forms.Label labelName;
      private System.Windows.Forms.TextBox textBoxName;
      private System.Windows.Forms.Button buttonOK;
      private System.Windows.Forms.TextBox textBoxAddress;
      private System.Windows.Forms.Label labelAddress;
      private System.Windows.Forms.Button buttonHelp;
      private System.Windows.Forms.Label labelAge;
      private System.Windows.Forms.TextBox textBoxAge;
      private System.Windows.Forms.Label labelOutput;
      private System.Windows.Forms.TextBox textBoxOutput;
      private System.Windows.Forms.CheckBox checkBoxProgrammer;
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.RadioButton radioButtonFemale;
      private System.Windows.Forms.RadioButton radioButtonMale;

      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
      }
   }
23.87.Validation
23.87.1.Validate text box value
23.87.2.Validation callback
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.