/// <summary>
        /// 检查是否存在;
        /// 控件所在窗体应有错误提供器,并通过名为ErrorProvider的公有属性访问;
        /// </summary>
        /// <param name="control">需要验证的控件</param>
        /// <param name="match">匹配条件</param>
        /// <param name="existIsValid">存在是否为有效</param>
        /// <returns>控件</returns>
        public static Control CheckExist(this Control control, Func <string, bool> match, bool existIsValid)
        {
            ValidatingUnit validatingUnit = new ValidatingUnit(control);

            validatingUnit.Match        = match;
            validatingUnit.ErrorMessage = existIsValid ? "不存在" : "已存在";
            return(control);
        }
        /// <summary>
        /// 文本长度范围;
        /// 控件所在窗体应有错误提供器,并通过名为ErrorProvider的公有属性访问;
        /// </summary>
        /// <param name="control">需要验证的控件</param>
        /// <param name="minLength">有效的最小文本长度</param>
        /// <param name="maxLength">有效的最大文本长度</param>
        /// <returns>控件</returns>
        public static Control LengthRange(this Control control, int minLength, int maxLength)
        {
            ValidatingUnit validatingUnit = new ValidatingUnit(control);

            validatingUnit.Match        = s => s.Length >= minLength && s.Length <= maxLength;
            validatingUnit.ErrorMessage = $"长度应为{minLength}~{maxLength}位";
            return(control);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 确认是否与另一控件的内容一致;
        /// </summary>
        /// <param name="confirmControl">需要验证的控件</param>
        /// <param name="referenceControl">需要参照的控件</param>
        /// <returns>控件</returns>
        public static Control Confirm(this Control confirmControl, Control referenceControl)
        {
            ValidatingUnit validatingUnit = new ValidatingUnit(confirmControl);

            validatingUnit.Match        = s => s == referenceControl.Text.Trim();
            validatingUnit.ErrorMessage = "两次输入不一致";
            return(confirmControl);
        }
        /// <summary>
        /// 非空;
        /// 控件所在窗体应有错误提供器,并通过名为ErrorProvider的公有属性访问;
        /// </summary>
        /// <param name="control">需要验证的控件</param>
        /// <returns>控件</returns>
        public static Control NotNull(this Control control)
        {
            ValidatingUnit validatingUnit = new ValidatingUnit(control);

            validatingUnit.Match        = s => !string.IsNullOrEmpty(s);
            validatingUnit.ErrorMessage = "不能为空";
            return(control);
        }
        /// <summary>
        /// 文本长度;允许指定多个文本长度;
        /// 控件所在窗体应有错误提供器,并通过名为ErrorProvider的公有属性访问;
        /// </summary>
        /// <param name="control">需要验证的控件</param>
        /// <param name="lengths">有效的文本长度</param>
        /// <returns>控件</returns>
        public static Control Length(this Control control, params int[] lengths)
        {
            ValidatingUnit validatingUnit = new ValidatingUnit(control);

            validatingUnit.Match =
                s =>
            {
                bool isValid = false;
                foreach (var length in lengths)
                {
                    isValid |= s.Length == length;
                }
                return(isValid);
            };
            string validLength = String.Empty;

            lengths.ToList().ForEach(i => validLength += i.ToString() + "或");
            validatingUnit.ErrorMessage = "长度应为" + validLength.Substring(0, validLength.Length - 1) + "位";
            return(control);
        }