zoukankan      html  css  js  c++  java
  • 窗体控件的验证

    自定义了一个窗体控件验证的类

    /// <summary>
        /// 窗体控件的验证
        /// </summary>
        public class WinFormValidating
        {
            /// <summary>
            /// 路径验证
            /// </summary>
            /// <returns></returns>
            public static bool validtePath(Control ctrl,ErrorProvider ep)
            {
                string pattern = @"^[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*$"; 
                Regex myRegex = new Regex(pattern);
                bool isR = myRegex.IsMatch(ctrl.Text);
                if (!isR)
                    ep.SetError(ctrl, "所输入的路径不合法");
                return isR;
            }
    
            /// <summary>
            /// 验证是否为空
            /// </summary>
            /// <param name="ctrl"></param>
            /// <param name="ep"></param>
            /// <returns></returns>
            public static bool validateNull(Control ctrl, ErrorProvider ep)
            {
                string content = ctrl.Text;
                if ((content == null) || content.Trim().Length == 0)
                {
                    ep.SetError(ctrl, "不能为空");
                    return false;
                }
                return true;
            }
    
            /// <summary>
            /// 验证窗体所有控件
            /// </summary>
            /// <param name="f1"></param>
            public static bool validateAll(Form f1)
            {
                //有一个控件没通过验证,返回false
                bool isR = true;
                foreach (Control ctrl in f1.Controls)
                {
                    ctrl.Focus();
                    bool tempB= f1.Validate();
                    if (!tempB)
                        isR = tempB;
                }
                return isR;
            }
    
            /// <summary>
            /// 验证整数
            /// </summary>
            /// <param name="ctrl"></param>
            /// <param name="ep"></param>
            /// <returns></returns>
            public static bool validateInteger(Control ctrl, ErrorProvider ep)
            {
                string context = ctrl.Text;
                string pattern = @"^([0-9]{1,})$";
                Regex myRegex = new Regex(pattern);
                bool isR = myRegex.IsMatch(context);
                if (!isR)
                    ep.SetError(ctrl, "所输入的不是有效的整数");
                return isR;
            }
    
            /// <summary>
            /// 验证正浮点数
            /// </summary>
            /// <param name="?"></param>
            /// <param name="ep"></param>
            /// <returns></returns>
            public static bool validatePositiveFloat(Control ctrl,ErrorProvider ep)
            {
                string context = ctrl.Text;
                string pattern = @"^[1-9]d*.d*|0.d*[1-9]d*$";
                Regex myRegex = new Regex(pattern);
                bool isR = myRegex.IsMatch(context);
                if (!isR)
                    ep.SetError(ctrl, "所输入的不是有效的整数");
                return isR;
            }
        }
    

     需要验证的文本框的验证事件示例如下:

      private void txtOutPath_Validating(object sender, CancelEventArgs e)
            {
                bool bt = WinFormValidating.validtePath(txtOutPath,errorProvider1);
                if (!bt)
                    e.Cancel = true;
            }
    
  • 相关阅读:
    tkinter TEXT
    tkinter
    threading.Event
    lambda demo
    Tomcat性能调优
    Tomcat优化
    BeautifulSoup库的使用
    正则的基本使用
    Urllib库的基本使用
    初识爬虫
  • 原文地址:https://www.cnblogs.com/DayDreamEveryWhere/p/2998700.html
Copyright © 2011-2022 走看看