winforms 下的控件有效性验证

     今天偶在 "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));
        }
    }
}

 

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
DotNetBar是一款带有56个 Windows Form 控件的工具箱,使开发人员可以轻而易举地创建出专业美观的Windows Form应用程序用户界面,控件全部采用C#编写,引入了全部Office 2007 style Ribbon控件、Office 2003 office2010 样式、支持windows7,Windows XP主题等。DotNetBar for WindowsForms 全面支持Office 2010 风格的后台应用程序菜单。 DotNetBar For Windows Forms 11.7.0.6(11706) 官方原版+注册机(强悍的用户界面控件库) DotNetBar For Windows Forms 11.7.0.6 官方原版+注册机(强悍的用户界面控件库) 具体功能: 为office2010、windows7、Office 2007 Ribbon 控件提供了Black,Silver ,Blue三种配色方案,支持Windows Vista Glass、tab groups, ribbon hyper menus, multi-level KeyTips, complete Quick Access Toolbar (QAT),支持Ribbon Menu Tabs Office 2007 Ribbon Bar 控件具有组合按钮,弹出按钮等 Office 2007 Super Tooltips控件具有18种颜色样式 支持停靠窗口自动隐藏,灵活的视图分割 支持具有Office 2007, VS.NET 2005, Office 2003/XP/2000,样式的Hyper-Menus 和工具条 支持具有Office 2007和2003样式的导航面板,支持扩展和收缩 SuperTab控件支持6种不同的样式,16种配色方案,支持颜色自定制 Explorer Bar控件使程序员可以创建出具有XP样式的Explorer Bar Tab-Strip控件具有九种预定义的样式,支持tabbed MDI ,颜色自定义 Side Bar 控件具有两种布局类型、5种不同的样式、18种预定义颜色方案,支持拖拉功能 BubbleBar 是一个灵活的toolbar控件 Stand alone Bar可以用于创建具有Office 2007 的状态条 可扩展的Splitter控件,Expandable Splitter控件能够直观的管理屏幕状态 可展开的Panel控件, Expandable Panel控件完整的支持office2007,2003的风格外观 多功能的Panel控件 Balloon/Alert控件用于传达一些重要的信息给最终用户 Wizard控件支持Wizard97规范,可以创建安装文件。 ButtonX 是一个卓越的按钮控件,ButtonX控件可以独立的控制下拉菜单、Office 2007, Office 2003, VS.NET 2005 和客户自定义颜色风格。 Color Picker控件可以创建具有Office 2007样式的下拉式颜色采集器,程序员也可以自定义颜色对话框 Item Panel使你可以创建出灵活的可以进行选择的多功能面板 具有Office 2007样式和外观的窗体 Office 2007样式的复选框和单选框 Progress bar控件可以用于创建具有Office 2007, Office 2003 和 VS.NET 2005的进度条 Office 2007样式的MessageBox 多功能Office 2007样式的TextBox 多功能Office 2007样式的下拉列表框 GroupPanel可以创建组合的TextBox文本框 Slider滚动条控件创建具有Office 2007样式的外观 具有Office 2007样式的ListViewEx控件,该控件不支持.NET Framework 1.0, 1.1. Office 2007样式的Label控件 DataGridViewX控件是一个表格控件,具有行、表头、滚动等功能,该控件不支持.NET Framework 1.0, 1.1. Date/Time Picker时间采集控件,可以用于创建具有Office 2007样式的日期选择控件 Month-Calendar控件让程序员可以创建月历控件 Double value input控件,用于控制值的输入,支持空值,货币值,小数,百分比 Integer value input控件可以控制整数的输入 Reflection Image控件为图片提供倒影功能 Reflection Label控件可以用于对Label提供倒影 水平和垂直滚动条控件可以创建Office 2007样式的滚动条 高级的可以指定格式输入的TextBox 多种功能的TreeView控件,支持拖拉功能,支持多选择 CrumbBar™ 控件 TextBoxDropDown控件 ComboBox树型控件可以用于在下拉列表框里创建树形控件 Rating控件 支持VS2013以及其他版本
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值