using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; namespace Stone.Common { public class MyErrorProvider { private static ErrorProvider errorProvider = new ErrorProvider(); static MyErrorProvider() { errorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink; } public static void SetError(Control control, string error) { errorProvider.SetError(control, error); control.Focus(); } public static void ClearError(Control control) { errorProvider.SetError(control, ""); } } public class MyValidator { /// /// 判断输入是否为空! /// /// /// /// public static bool IsEmpty(Control control, string error) { if (control.Text.Trim() == string.Empty) { MyErrorProvider.SetError(control, error + " 不能为空!"); return false; } MyErrorProvider.ClearError(control); return true; } /// /// 判断输入是否是数值 /// /// /// /// public static bool IsNumeric(Control control, string error) { bool result; Regex regex = new Regex("^[0-9]*[0-9][0-9]*$"); result = regex.IsMatch(control.Text.Trim()); if (!result) { MyErrorProvider.SetError(control, error + " 只能是数字!"); return result; } MyErrorProvider.ClearError(control); return result; } /// /// 判断输入是否为Money /// /// /// /// public static bool IsMoney(Control control, string error) { bool result; result = false; try { double t = Convert.ToDouble(control.Text.Trim()); if (t < 0) { result = false; }else { result = true; } } catch { } if (!result) { MyErrorProvider.SetError(control, error + " 不是正确的金额!"); return result; } MyErrorProvider.ClearError(control); return result; } } }