winforms 下的控件有效性验证

来源:互联网 发布:shadowsock mac 编辑:程序博客网 时间:2024/05/17 23:17

     今天偶在 "C#经典入门"上看了一个关于TextBox的例子,发现winforms中对控件的有效性验证比Web难多了,主要是因为它涉及到了自定义事件,而web应用程序的开发对控件的验证,VS工具提供了相应的验证控件。另外对Tag属性也有了一定的认识,当你使用这个属性时,会有很多意想不到的效果。感觉这个Tag属性,可以当成是一个控件的标签或标志,或者用来作为控件的备注。

通过这个程序的学习我对事件也有了更进一步的认识。顺便也把这个程序拿出来给大家分享一下。

界面设计

namespace TextBoxTest
{
    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.lblName = new System.Windows.Forms.Label();
            this.lblAddress = new System.Windows.Forms.Label();
            this.lblOccupation = new System.Windows.Forms.Label();
            this.lblAge = new System.Windows.Forms.Label();
            this.lblOutput = new System.Windows.Forms.Label();
            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.btnOK = new System.Windows.Forms.Button();
            this.btnHelp = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lblName
            //
            this.lblName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.lblName.AutoSize = true;
            this.lblName.Location = new System.Drawing.Point(26, 30);
            this.lblName.Name = "lblName";
            this.lblName.Size = new System.Drawing.Size(47, 12);
            this.lblName.TabIndex = 0;
            this.lblName.Text = "lblName";
            //
            // lblAddress
            //
            this.lblAddress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.lblAddress.AutoSize = true;
            this.lblAddress.Location = new System.Drawing.Point(26, 72);
            this.lblAddress.Name = "lblAddress";
            this.lblAddress.Size = new System.Drawing.Size(65, 12);
            this.lblAddress.TabIndex = 1;
            this.lblAddress.Text = "lblAddress";
            //
            // lblOccupation
            //
            this.lblOccupation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.lblOccupation.AutoSize = true;
            this.lblOccupation.Location = new System.Drawing.Point(26, 178);
            this.lblOccupation.Name = "lblOccupation";
            this.lblOccupation.Size = new System.Drawing.Size(83, 12);
            this.lblOccupation.TabIndex = 2;
            this.lblOccupation.Text = "lblOccupation";
            //
            // lblAge
            //
            this.lblAge.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.lblAge.AutoSize = true;
            this.lblAge.Location = new System.Drawing.Point(26, 231);
            this.lblAge.Name = "lblAge";
            this.lblAge.Size = new System.Drawing.Size(41, 12);
            this.lblAge.TabIndex = 3;
            this.lblAge.Text = "lblAge";
            //
            // lblOutput
            //
            this.lblOutput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.lblOutput.AutoSize = true;
            this.lblOutput.Location = new System.Drawing.Point(26, 308);
            this.lblOutput.Name = "lblOutput";
            this.lblOutput.Size = new System.Drawing.Size(59, 12);
            this.lblOutput.TabIndex = 4;
            this.lblOutput.Text = "lblOutput";
            //
            // txtName
            //
            this.txtName.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.txtName.Location = new System.Drawing.Point(115, 30);
            this.txtName.MinimumSize = new System.Drawing.Size(100, 21);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(100, 21);
            this.txtName.TabIndex = 5;
            //
            // txtAddress
            //
            this.txtAddress.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.txtAddress.Location = new System.Drawing.Point(115, 72);
            this.txtAddress.MaximumSize = new System.Drawing.Size(298, 73);
            this.txtAddress.MinimumSize = new System.Drawing.Size(298, 73);
            this.txtAddress.Multiline = true;
            this.txtAddress.Name = "txtAddress";
            this.txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtAddress.Size = new System.Drawing.Size(298, 73);
            this.txtAddress.TabIndex = 6;
            //
            // txtOccupation
            //
            this.txtOccupation.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.txtOccupation.Location = new System.Drawing.Point(115, 178);
            this.txtOccupation.MinimumSize = new System.Drawing.Size(100, 21);
            this.txtOccupation.Name = "txtOccupation";
            this.txtOccupation.Size = new System.Drawing.Size(100, 21);
            this.txtOccupation.TabIndex = 7;
            //
            // txtAge
            //
            this.txtAge.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.txtAge.Location = new System.Drawing.Point(115, 231);
            this.txtAge.Name = "txtAge";
            this.txtAge.Size = new System.Drawing.Size(100, 21);
            this.txtAge.TabIndex = 8;
            //
            // txtOutput
            //
            this.txtOutput.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.txtOutput.Location = new System.Drawing.Point(115, 308);
            this.txtOutput.MaximumSize = new System.Drawing.Size(387, 150);
            this.txtOutput.MinimumSize = new System.Drawing.Size(387, 150);
            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(387, 150);
            this.txtOutput.TabIndex = 9;
            //
            // btnOK
            //
            this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnOK.Location = new System.Drawing.Point(508, 30);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 10;
            this.btnOK.Text = "OK";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            //
            // btnHelp
            //
            this.btnHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnHelp.CausesValidation = false;
            this.btnHelp.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnHelp.Location = new System.Drawing.Point(508, 90);
            this.btnHelp.Name = "btnHelp";
            this.btnHelp.Size = new System.Drawing.Size(75, 23);
            this.btnHelp.TabIndex = 11;
            this.btnHelp.Text = "Help";
            this.btnHelp.UseVisualStyleBackColor = true;
            this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(666, 465);
            this.Controls.Add(this.btnHelp);
            this.Controls.Add(this.btnOK);
            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.lblOutput);
            this.Controls.Add(this.lblAge);
            this.Controls.Add(this.lblOccupation);
            this.Controls.Add(this.lblAddress);
            this.Controls.Add(this.lblName);
            this.MinimumSize = new System.Drawing.Size(674, 499);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        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.Label lblOutput;
        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.Button btnOK;
        private System.Windows.Forms.Button btnHelp;
    }
}

//实现代码

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

namespace TextBoxTest
{
    public partial class Form1 : Form
    {
        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(txtBoxEmpty_Validating);
            this.txtAddress.Validating +=
                new System.ComponentModel.CancelEventHandler(txtBoxEmpty_Validating);

            this.txtOccupation.Validating +=
                new System.ComponentModel.CancelEventHandler(txtOccupation_Validating);

            this.txtAge.KeyPress +=
                new System.Windows.Forms.KeyPressEventHandler(txtAge_KeyPress);

            this.txtAge.Validating +=
                new System.ComponentModel.CancelEventHandler(txtBoxEmpty_Validating);

            this.txtAddress.TextChanged +=
                new System.EventHandler(this.txtBox_TextChanged);
            this.txtAge.TextChanged +=
                 new System.EventHandler(this.txtBox_TextChanged);
            this.txtName.TextChanged +=
                 new System.EventHandler(this.txtBox_TextChanged);
            this.txtOccupation.TextChanged +=
                 new System.EventHandler(this.txtBox_TextChanged);
        }

        /// <summary>
        /// OK按钮响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            string output;

            output = "Name: " + txtName.Text + "/r/n";
            output += "Address: " + txtAddress.Text + "/r/n";
            output += "Occupation: " + txtOccupation.Text + "/r/n";
            output += "Age: " + txtAge.Text + "/r/n";

            this.txtOutput.Text = output;
        }


        /// <summary>
        /// Help按钮响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHelp_Click(object sender, EventArgs e)
        {
            string output;

            output = "Name = Your name/r/n";
            output += "Address = Your address/r/n";
            output += "Occupation = Only allowed value is 'Programmer'";
            output += "Age = Your age";

            this.txtOutput.Text = output;
        }


        /// <summary>
        /// TextBox为空的验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBoxEmpty_Validating(object sender,
                        System.ComponentModel.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;
            }

            ValidateAll();
        }


        /// <summary>
        /// 对Occupation_Validating控件的验证(只能填入Programmer或者为空)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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();
        }


        /// <summary>
        /// 对txtAge填入年龄的验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtAge_KeyPress(object sender,
            System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }


        /// <summary>
        /// TextBox内容改变时的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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.CompareTo("Programmer") != 0)
            {
                tb.Tag = false;
            }
            else
            {
                tb.Tag = true;
                tb.BackColor = SystemColors.Window;
            }
            ValidateAll();
        }


        /// <summary>
        /// 判断所有控件是否都通过了验证
        /// </summary>
        private void ValidateAll()
        {
            this.btnOK.Enabled = ((bool)(txtAddress.Tag) &&
                                    (bool)(txtAge.Tag) &&
                                    (bool)(txtName.Tag) &&
                                    (bool)(txtOccupation.Tag));
        }
    }
}

 

 


 

 

原创粉丝点击