zoukankan      html  css  js  c++  java
  • 3.C# TextBox扩展控件可以自定义验证属性

    1.解决方案下添加新建项目新建类库
    2. 在项目下添加新建项选择新建组件类
    3.先引用,然后导入两个命名空间
    4.因为是扩展控件,把继承自Component改成继承自TextBox
      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Diagnostics;
      5 using System.Linq;
      6 using System.Text;
      7 using System.Windows.Forms;
      8 using System.Drawing;
      9 using System.Text.RegularExpressions;
     10 
     11 namespace FJZControl
     12 {
     13     public partial class FJZTextBox :TextBox
     14     {
     15         public FJZTextBox()
     16         {
     17             InitializeComponent();
     18             this.BorderStyle = BorderStyle.FixedSingle;
     19             
     20         }
     21 
     22         public FJZTextBox(IContainer container)
     23         {
     24             container.Add(this);
     25 
     26             InitializeComponent();
     27         }
     28 
     29         #region 属性
     30         private string regularExpress;
     31         private string errorMsg;
     32         [Category("FJZ的TextBox自定义控件属性")]
     33         [Description("需要验证的正则表达式")]
     34         public string RegularExpress
     35         {
     36             get
     37             {
     38                 return regularExpress;
     39             }
     40 
     41             set
     42             {
     43                 regularExpress = value;
     44             }
     45         }
     46         [Category("FJZ的TextBox自定义控件属性")]
     47         [Description("验证提示的错误信息")]
     48         public string ErrorMsg
     49         {
     50             get
     51             {
     52                 return errorMsg;
     53             }
     54 
     55             set
     56             {
     57                 errorMsg = value;
     58             }
     59         }
     60 
     61         #endregion
     62         //扩展一个非空验证的功能
     63         private int BeginCheckEmpty()
     64         {
     65             if (this.Text.Trim().Length == 0)
     66             {
     67                 errorProvider.SetError(this, "必填项不能为空");
     68                 return 0;//0代表验证不通过
     69             }
     70             else
     71             {
     72                 errorProvider.SetError(this, string.Empty);//清除小圆点的提示
     73                 return 1;
     74             }
     75         }
     76 
     77         /// <summary>
     78         /// 通用正则表达式验证(可以实现非常复杂的数据格式验证)
     79         /// </summary>
     80         /// <param name="regularExpress">正则表达式</param>
     81         /// <param name="errorMsg">错误信息</param>
     82         /// <returns></returns>
     83         public int BeginCommonValidate()
     84         {
     85             if (BeginCheckEmpty() == 0) return 0;//如果为零,直接返回
     86                                                  //创建正则表达式对象
     87             Regex regex = new Regex(this.regularExpress);
     88             if (regex.IsMatch(this.Text.Trim()))
     89             {
     90                 this.errorProvider.SetError(this, string.Empty);
     91                 return 1;
     92             }
     93             else
     94             {
     95                 this.errorProvider.SetError(this, this.errorMsg);
     96                 return 0;
     97              
     98             }
     99 
    100         }
    101     }
    102 }
    View Code
    效果如下:非空验证和自定义验证
  • 相关阅读:
    【重学计算机】计组D2章:数据表示
    【重学计算机】计组D1章:计算机系统概论
    计算机底层原理杂谈(白话文)
    阿里云安装wordpress遇到的问题
    wordpress数据表结构
    家用计费系统ER图
    java 类中的属性为什么一般都是私有的
    centos 软件库安装
    linux下启动tomcat----Cannot find ./catalina.sh
    jfreechart图表汉字乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/fanjianzhi/p/12907950.html
Copyright © 2011-2022 走看看