//加重部分 定义代理
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Calculator
{
public class Form1 : System.Windows.Forms.Form
{
#region DETAILS...
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtNumber1;
private System.Windows.Forms.Button cmdAdd;
private System.Windows.Forms.TextBox txtNumber2;
private System.Windows.Forms.Button cmdEnable;
private System.Windows.Forms.Button cmdDisable;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtNumber1 = new System.Windows.Forms.TextBox();
this.txtNumber2 = new System.Windows.Forms.TextBox();
this.cmdAdd = new System.Windows.Forms.Button();
this.cmdEnable = new System.Windows.Forms.Button();
this.cmdDisable = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 32);
this.label1.TabIndex = 0;
this.label1.Text = "Number 1:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 32);
this.label2.TabIndex = 2;
this.label2.Text = "Number 2:";
//
// txtNumber1
//
this.txtNumber1.Location = new System.Drawing.Point(152, 24);
this.txtNumber1.Name = "txtNumber1";
this.txtNumber1.Size = new System.Drawing.Size(160, 29);
this.txtNumber1.TabIndex = 1;
this.txtNumber1.Text = "";
//
// txtNumber2
//
this.txtNumber2.Location = new System.Drawing.Point(152, 72);
this.txtNumber2.Name = "txtNumber2";
this.txtNumber2.Size = new System.Drawing.Size(160, 29);
this.txtNumber2.TabIndex = 3;
this.txtNumber2.Text = "";
//
// cmdAdd
//
this.cmdAdd.Location = new System.Drawing.Point(168, 120);
this.cmdAdd.Name = "cmdAdd";
this.cmdAdd.Size = new System.Drawing.Size(128, 56);
this.cmdAdd.TabIndex = 4;
this.cmdAdd.Text = "Add";
this.cmdAdd.Click += new System.EventHandler(this.cmdAdd_Click);
//
// cmdEnable
//
this.cmdEnable.CausesValidation = false;
this.cmdEnable.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.cmdEnable.Location = new System.Drawing.Point(24, 280);
this.cmdEnable.Name = "cmdEnable";
this.cmdEnable.Size = new System.Drawing.Size(272, 48);
this.cmdEnable.TabIndex = 6;
this.cmdEnable.TabStop = false;
this.cmdEnable.Text = "Enable Validation";
this.cmdEnable.Click += new System.EventHandler(this.cmdEnable_Click);
//
// cmdDisable
//
this.cmdDisable.CausesValidation = false;
this.cmdDisable.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.cmdDisable.Location = new System.Drawing.Point(24, 216);
this.cmdDisable.Name = "cmdDisable";
this.cmdDisable.Size = new System.Drawing.Size(272, 48);
this.cmdDisable.TabIndex = 5;
this.cmdDisable.TabStop = false;
this.cmdDisable.Text = "Disable Validation";
this.cmdDisable.Click += new System.EventHandler(this.cmdDisable_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(10, 22);
this.ClientSize = new System.Drawing.Size(338, 352);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdDisable,
this.cmdEnable,
this.cmdAdd,
this.txtNumber2,
this.txtNumber1,
this.label2,
this.label1});
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Calculator";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void cmdAdd_Click(object sender, System.EventArgs e)
{
int num1, num2, result;
num1 = int.Parse(this.txtNumber1.Text);
num2 = int.Parse(this.txtNumber2.Text);
result = num1 + num2;
MessageBox.Show("The sum = " + result, "Calculator");
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.txtNumber1.Validating += new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
this.txtNumber2.Validating += new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
// validation is already enabled, so prevent user from adding more validation handlers
this.cmdEnable.Enabled = false;
}
private void cmdDisable_Click(object sender, System.EventArgs e)
{
this.txtNumber1.Validating -= new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
this.txtNumber2.Validating -= new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
// toggle buttons so user can enable later...
this.cmdDisable.Enabled = false;
this.cmdEnable.Enabled = true;
}
private void cmdEnable_Click(object sender, System.EventArgs e)
{
this.txtNumber1.Validating += new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
this.txtNumber2.Validating += new System.ComponentModel.CancelEventHandler(Validation.IsInteger);
// toggle buttons so user can disable later...
this.cmdEnable.Enabled = false;
this.cmdDisable.Enabled = true;
}
}
}
//定义代理函数
using System;
using SWF = System.Windows.Forms;
namespace Calculator
{
public class Validation
{
public static void IsInteger(object sender, System.ComponentModel.CancelEventArgs e)
{
SWF.TextBox txt;
txt = (SWF.TextBox) sender;
try
{
int.Parse(txt.Text);
}
catch(FormatException)
{
SWF.MessageBox.Show("Please enter a numeric value.", "Calculator");
e.Cancel = true; // cancel validation, returns focus
}
catch(OverflowException)
{
SWF.MessageBox.Show("Please enter an integer in the range " + int.MinValue + " ... " + int.MaxValue, "Calculator");
e.Cancel = true; // cancel validation, returns focus
}
catch(Exception ex)
{
SWF.MessageBox.Show("Unknown error: " + ex.Message, "Calculator");
e.Cancel = true; // cancel validation, returns focus
}
}
}//class
}//namespace