using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();          
        }
        private void btnOK_Click(object sender, 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;
            this.txtOutput.Text = output;
        }
        private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventHandler 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;           
            }
            ValidateAll(); 
           }
        private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.CompareTo("Programmer") == 0 || 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.KeyPressEventHandler e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;            
        }
        private void txtBox_TextChanged(object sender, System.EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if(tb.Text.Length==0&&tb!=txtOccupation)
            {
                tb.Tag=false;
                tb.BackColor=Color.Red;
            }
            else if(tb==txtOccupation&&(tb.Text.Length!=0&&tb.Text.CompareTo("Programmer")!=0))
            {
                tb.Tag=true;
                tb.BackColor=SystemColors.Window;
            }
            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 btnHelp_Click(object sender, EventArgs e)
        {
            string output;
            output = "Name=your name\r\n";
            output += "Address=Your address\r\n";
            output += "Occpation=Only allowed value is 'Programmer'\r\n";
            output += "Ag=Your age";
            this.txtOutput.Text = output;           
        }
    }
}
----------------------------------------------------------------------------------------错误 1 “System.Windows.Forms.KeyPressEventHandler”并不包含“KeyChar”的定义 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.cs 58 20 WindowsApplication5
错误 2 “System.Windows.Forms.KeyPressEventHandler”并不包含“KeyChar”的定义 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.cs 58 38 WindowsApplication5
错误 3 “System.Windows.Forms.KeyPressEventHandler”并不包含“KeyChar”的定义 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.cs 58 57 WindowsApplication5
错误 4 “System.Windows.Forms.KeyPressEventHandler”并不包含“Handled”的定义 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.cs 59 19 WindowsApplication5
错误 5 “txtBoxEmpty_Validating”的重载均与委托“System.ComponentModel.CancelEventHandler”不匹配 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.Designer.cs 75 40 WindowsApplication5
错误 6 “txtBoxEmpty_Validating”的重载均与委托“System.ComponentModel.CancelEventHandler”不匹配 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.Designer.cs 87 43 WindowsApplication5
错误 7 “txtAge_KeyPress”的重载均与委托“System.Windows.Forms.KeyPressEventHandler”不匹配 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.Designer.cs 107 37 WindowsApplication5
错误 8 “txtBoxEmpty_Validating”的重载均与委托“System.ComponentModel.CancelEventHandler”不匹配 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WindowsApplication5\WindowsApplication5\Form1.Designer.cs 108 39 WindowsApplication5----------------------------------------------------------------------------------------

解决方案 »

  1.   

    namespace WindowsApplication5
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()        {
                this.btnOK = new System.Windows.Forms.Button();
                this.btnHelp = new System.Windows.Forms.Button();
                this.txtName = new System.Windows.Forms.TextBox();
                this.txtAddress = new System.Windows.Forms.TextBox();
                this.txtOccupation = new System.Windows.Forms.TextBox();
                this.txtAge = new System.Windows.Forms.TextBox();
                this.txtOutput = new System.Windows.Forms.TextBox();
                this.lblName = new System.Windows.Forms.Label();
                this.lblAddress = new System.Windows.Forms.Label();
                this.lblAge = new System.Windows.Forms.Label();
                this.lblOccupation = new System.Windows.Forms.Label();
                this.lblOutput = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // btnOK
                // 
                this.btnOK.Enabled = false;
                this.btnOK.Location = new System.Drawing.Point(332, 12);
                this.btnOK.Name = "btnOK";
                this.btnOK.Size = new System.Drawing.Size(75, 23);
                this.btnOK.TabIndex = 0;
                this.btnOK.Text = "OK";
                this.btnOK.UseVisualStyleBackColor = true;
                this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
                // 
                // btnHelp
                // 
                this.btnHelp.Location = new System.Drawing.Point(332, 54);
                this.btnHelp.Name = "btnHelp";
                this.btnHelp.Size = new System.Drawing.Size(75, 23);
                this.btnHelp.TabIndex = 1;
                this.btnHelp.Text = "Help";
                this.btnHelp.UseVisualStyleBackColor = true;
                this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
                this.btnHelp.CausesValidationChanged += new System.EventHandler(this.btnHelp_Click);
                // 
                // txtName
                // 
                this.txtName.Location = new System.Drawing.Point(112, 12);
                this.txtName.Name = "txtName";
                this.txtName.Size = new System.Drawing.Size(196, 21);
                this.txtName.TabIndex = 2;
                this.txtName.Tag = false;
                this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
                this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
                // 
                // txtAddress
                // 
                this.txtAddress.Location = new System.Drawing.Point(112, 32);
                this.txtAddress.Multiline = true;
                this.txtAddress.Name = "txtAddress";
                this.txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.txtAddress.Size = new System.Drawing.Size(196, 103);
                this.txtAddress.TabIndex = 3;
                this.txtAddress.Tag = false;
                this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
                this.txtAddress.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
                // 
                // txtOccupation
                // 
                this.txtOccupation.Location = new System.Drawing.Point(112, 133);
                this.txtOccupation.Name = "txtOccupation";
                this.txtOccupation.Size = new System.Drawing.Size(196, 21);
                this.txtOccupation.TabIndex = 4;
                this.txtOccupation.Tag = false;
                this.txtOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
                this.txtOccupation.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
                // 
                // txtAge
                // 
                this.txtAge.Location = new System.Drawing.Point(112, 153);
                this.txtAge.Name = "txtAge";
                this.txtAge.Size = new System.Drawing.Size(196, 21);
                this.txtAge.TabIndex = 5;
                this.txtAge.Tag = false;
                this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
                this.txtAge.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
                this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
                // 
                // txtOutput
                // 
                this.txtOutput.Location = new System.Drawing.Point(112, 173);
                this.txtOutput.Multiline = true;
                this.txtOutput.Name = "txtOutput";
                this.txtOutput.ReadOnly = true;
                this.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.txtOutput.Size = new System.Drawing.Size(196, 137);
                this.txtOutput.TabIndex = 6;
                // 
                // lblName
                // 
                this.lblName.AutoSize = true;
                this.lblName.Location = new System.Drawing.Point(6, 12);
                this.lblName.Name = "lblName";
                this.lblName.Size = new System.Drawing.Size(29, 12);
                this.lblName.TabIndex = 7;
                this.lblName.Text = "Name";
                // 
                // lblAddress
                // 
                this.lblAddress.AutoSize = true;
                this.lblAddress.Location = new System.Drawing.Point(6, 32);
                this.lblAddress.Name = "lblAddress";
                this.lblAddress.Size = new System.Drawing.Size(47, 12);
                this.lblAddress.TabIndex = 8;
                this.lblAddress.Text = "Address";
      

  2.   

    // 
                // lblAge
                // 
                this.lblAge.AutoSize = true;
                this.lblAge.Location = new System.Drawing.Point(6, 123);
                this.lblAge.Name = "lblAge";
                this.lblAge.Size = new System.Drawing.Size(23, 12);
                this.lblAge.TabIndex = 9;
                this.lblAge.Text = "Age";
                // 
                // lblOccupation
                // 
                this.lblOccupation.AutoSize = true;
                this.lblOccupation.Location = new System.Drawing.Point(6, 153);
                this.lblOccupation.Name = "lblOccupation";
                this.lblOccupation.Size = new System.Drawing.Size(65, 12);
                this.lblOccupation.TabIndex = 10;
                this.lblOccupation.Text = "Occupation";
                // 
                // lblOutput
                // 
                this.lblOutput.AutoSize = true;
                this.lblOutput.Location = new System.Drawing.Point(6, 173);
                this.lblOutput.Name = "lblOutput";
                this.lblOutput.Size = new System.Drawing.Size(41, 12);
                this.lblOutput.TabIndex = 11;
                this.lblOutput.Text = "Output";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(417, 331);
                this.Controls.Add(this.lblOutput);
                this.Controls.Add(this.lblOccupation);
                this.Controls.Add(this.lblAge);
                this.Controls.Add(this.lblAddress);
                this.Controls.Add(this.lblName);
                this.Controls.Add(this.txtOutput);
                this.Controls.Add(this.txtAge);
                this.Controls.Add(this.txtOccupation);
                this.Controls.Add(this.txtAddress);
                this.Controls.Add(this.txtName);
                this.Controls.Add(this.btnHelp);
                this.Controls.Add(this.btnOK);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        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;
            private System.Windows.Forms.TextBox txtOutput;
            private System.Windows.Forms.Label lblName;
            private System.Windows.Forms.Label lblAddress;
            private System.Windows.Forms.Label lblAge;
            private System.Windows.Forms.Label lblOccupation;
            private System.Windows.Forms.Label lblOutput;
        }
    }
      

  3.   

    private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventHandler e)改为:private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    ----------------------------------------------------------------
    private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventHandler e)改为:private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)