当前位置:编程学习 > C#/ASP.NET >>

C# Windows Form下的控件的Validator(数据验证)

由于偶尔的一个想法,谋生了一个做一个windows form下的Validator控件,或者直接说类吧!
因为webform下的Validator控件太好用了。哈哈,直接看代码!
 
下面这个类,主要是一个简单的验证类,不过只是起到一个抛砖引玉的作用,其他的功能,大家发挥想象吧!
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows.Forms;
  6
  7 namespace FinanceManager.Code
  8 {
  9     /// <summary>
 10 /// 输入验证类
 11 /// </summary>
 12     public class Validator
 13     {
 14         /// <summary>
 15 /// 判断指定文本框内容是否满足条件
 16 /// </summary>
 17 /// <param name="textBox">需要验证的文本框</param>
 18 /// <param name="type">指定验证类型</param>
 19 /// <param name="LabelDisplayError">需要显示错误的标签</param>
 20 /// <param name="lenth">用于验证文本框内字符的长度</param>
 21 /// <returns>满足条件返回True,不满足条件返回False</returns>
 22         public static Boolean ValidTextBox(TextBox textBox, ValidType type, Label LabelDisplayError, Int32 lenth = 10)
 23         {
 24             if (LabelDisplayError == null)
 25             {
 26                 return ValidTextBox(textBox, type, lenth);
 27             }
 28             else
 29             {
 30                 if (!ValidTextBox(textBox, type, lenth))
 31                 {
 32                     LabelDisplayError.Visible = true;
 33                     return ValidTextBox(textBox, type, lenth);
 34                 }
 35                 else
 36                 {
 37                     LabelDisplayError.Visible = false;
 38                     return ValidTextBox(textBox, type, lenth);
 39                 }
 40             }
 41         }
 42         /// <summary>
 43 /// 判断指定文本框内容是否满足条件
 44 /// </summary>
 45 /// <param name="textBox">需要验证的文本框</param>
 46 /// <param name="type">指定验证类型</param>
 47 /// <param name="lenth">用于验证文本框内字符的长度</param>
 48 /// <returns>满足条件返回True,不满足条件返回False</returns>
 49         public static Boolean ValidTextBox(TextBox textBox, ValidType type, Int32 lenth = 10)
 50         {
 51             Boolean flag = false;
 52             switch (type)
 53             {
 54                 case ValidType.Required:
 55                     if (!String.IsNullOrEmpty(textBox.Text.Trim()))
 56                     {
 57                         flag = true;
 58                     }
 59                     else
 60                     {
 61                         flag = false;
 62                     }
 63                     break;
 64                 case ValidType.CharLength:
 65                     if (!String.IsNullOrEmpty(textBox.Text.Trim()))
 66                     {
 67                         if (textBox.Text.Trim().Length < lenth)
 68                         {
 69                             flag = true;
 70                         }
 71                         else
 72                         {
 73                     &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,