这里我们创建2个TextBox 控件,应用WinForm的验证机制使得TextBox 只能输入数字否则提示错误并且不允许鼠标焦点切换 。
ValidateForm.Designer.cs代码如下:
namespace validateTextForm

...{
partial class ValidateForm

...{

/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)

...{
if (disposing && (components != null))

...{
components.Dispose();
}
base.Dispose(disposing);
}


Windows Form Designer generated code#region Windows Form Designer generated code


/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

...{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
this.errorProvider2 = new System.Windows.Forms.ErrorProvider(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.errorProvider2)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(30, 21);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(75, 22);
this.textBox1.TabIndex = 0;
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(23, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(243, 97);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Validate";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(140, 21);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(75, 22);
this.textBox2.TabIndex = 1;
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// errorProvider2
//
this.errorProvider2.ContainerControl = this;
//
// button1
//
this.button1.Location = new System.Drawing.Point(30, 61);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Validate";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(140, 61);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Reset";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(288, 131);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Validating";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.errorProvider2)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.ErrorProvider errorProvider1;
private System.Windows.Forms.ErrorProvider errorProvider2;
private System.Boolean Flag = false;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
}
}


ValidateForm.cs代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace validateTextForm

...{
public partial class ValidateForm : Form

...{
public ValidateForm()

...{
InitializeComponent();
}

private void textBox1_Validating(object sender, CancelEventArgs e)

...{
Int32 intValue;
if (!Int32.TryParse(textBox1.Text, out intValue))

...{
errorProvider1.SetError(textBox1, "must be integer !");
Flag = true;
e.Cancel = true;
}
else

...{
errorProvider1.SetError(textBox1,null);
}
}

private void textBox2_Validating(object sender, CancelEventArgs e)

...{
Int32 intValue;
if (!Int32.TryParse(textBox2.Text, out intValue))

...{
errorProvider2.SetError(textBox2, "must be integer !");
Flag = true;
e.Cancel = true;
}
else

...{
errorProvider2.SetError(textBox2, null);
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

...{
if (Flag == true)

...{
e.Cancel = false;
}
}

private void button1_Click(object sender, EventArgs e)

...{
MessageBox.Show("Validating Successful !");
}

private void button2_Click(object sender, EventArgs e)

...{
textBox1.Text = textBox2.Text = String.Empty;
}
}
}